npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-auto-save

v1.1.0

Published

Set of angularjs directives to save inputs' values as user types

Downloads

6

Readme

ng-auto-save

Set of angularjs directives to save inputs' values as user types

Installation:

via NPM: npm install ng-auto-save

Reference module in your app

angular.module('app', ['ng-auto-save']);

Demo:

Plunker Live Demo - Exchange with the database is imitated.

Example:

All you have to do in the controller is to provide a function to save record. This must return a promise and it takes three arguments:

  • field - corresponds to the column in the table in database that will be updated
  • val - new value to save
  • key - the constraint that usually goes in the the where clause of the sql update statement

index.js - inside Controller:

$scope.updateField = function (field, val, key) {
    return $http.post('/api/article/update', { id: key, field: field, val: val })
        .success(function () {
            articleRef[field] = $scope.article[field];
        })
        .error(function(error) {
            console.log(error);
        });
};

index.html: Example uses angular-material, ng-messages, font-awesome

<form name="formEditArticle" novalidate auto-save="updateField" auto-save-key="article.id" auto-save-debounce="1000">
    <md-content layout="column">
        <div layout="row" layout-align="start center">
            <md-input-container>
                <label>Name</label>
                <input ng-model="article.name" required auto-save-field="name" name="article.name">
            </md-input-container>
            <span class="fa fa-gear fa-spin" auto-saving="name"></span>
            <span class="fa fa-check-square-o" auto-saved="name"></span>
        </div>

        <div ng-messages for="formEditArticle['article.name'].$error">
            <span ng-message when="required">Name is required</span>
        </div>
    </md-content>
</form>

Explanation of directives used:

  • auto-save="updateField" - must be applied to the <form>. It takes the name of the function used to save record.
  • auto-save-key="article.id" - article.id here is the primary key in the table Articles. If it is undefined, the auto-save function will not execute (thus making it easy to customize insert functionality).
  • auto-save-debounce="1000" - specify how long to wait for input before saving
  • auto-save-field="name" - must be applied to an input with attribute ng-model. The "name" here is the name of the column in the table to be updated.
  • auto-saving="name" - apply this directive to an element that you want to show up when saving is in progress. The value of the attribute must be set to the value of 'auto-save-field' applied to the corresponding input
  • auto-saved="name" - apply this directive to an element that you want to show up when saving is in complete successfully. The value of the attribute must be set to the value of 'auto-save-field' applied to the corresponding input

Server side implementation Example for NodeJs as a web server using express.js and PostgreSql as a database.

Set Route:

router.post('/api/article/update', routes.api.articleUpdateField);

Implementation for route:

var query = require('pg-query');
var squel = require("squel");
squel.useFlavour('postgres');
var sqlOptions = { autoQuoteAliasNames: false };
query.connectionParameters = 'postgres://postgres:mypassword@localhost:5432/Articles'; // example connection

exports.articleUpdateField = function (req, res) {
    var id = parseInt(req.body.id);

    if (id) {
        var sql = squel.update()
            .table('tblArticles')
            .set(req.body.field, req.body.val)
            .where('id = ?', id)
            .toParam();

        query(sql.text, sql.values, function(err, rows, result) {
            if (err) {
                console.log(err);
                res.status(500).end();
            } else {
                res.end();
            }
        });
    } else {
        res.status(500).end();
    }
};

Credits

Inspired by an article written by Adam Albrecht: "How to Auto-Save your model in Angular.js using $watch and a Debounce function."