Using NProgress with Backbone.js

10 months ago

Here is a little convenient method to show a progress indicator whenever one or more collections or models are making a request, without the need to worry about setting up listeners for each of them.

In this example I’m using Marionette’s Backbone.Wreqr to setup an application wide reqres handler for handling NProgress.

  
  var App = new Marionette.Application();
  
  // Keep track of the pending promises, 
  // this way the indicator is only stopped when all pending promises are done.
  var promises = 0;
  App.reqres.setHandler('progress', function(promise) {
    promises++;
    NProgress.start();
    
    // Return the promise to keep chaining support.
    return promise
      // Increase the progress indicator when we receive a progress notification.
      .progress(function() {
        NProgress.inc();
      })
      // Use always here so it doesn't matter if the promise is resolved or rejected.
      .always(function() {
        promises--;
        !promises && NProgress.done(); // No more pending promises, we're done.
      });
  });

  // See http://backbonejs.org/#Sync-ajax
  Backbone.ajax = function() {
    // Wrap all ajax requests in our progress handler.
    return App.request('progress', Backbone.$.ajax.apply(Backbone.$, arguments));
  };
  

Set Underscore.js’ templateSettings with RequireJS

1 year ago

Overriding _.template settings in your modules can become a bit tedious, for example when you want to use the ‘variable’ setting to improve performance or when you prefer a different template style.

Updating the default template settings (_.templateSettings) is pretty straight forward, however when using RequireJS your changes won’t be available in your modules. To fix this you can change the underscore export in your shim configuration, like this:

require.config({
  shim: {
    underscore: {
      exports: '_',
      init: function () {
        this._.extend(this._.templateSettings, {
          variable: 'data'
        });
        return this._;
      }
    }
  }
});

SoapSource added into the Cakephp datasources repository

4 years ago

Today my Soap datasource has been added into the Cakephp datasources repository by Graham Weldon.
Great to see my work got accepted.

Book review: CakePHP Application Development

5 years ago

Just like many other CakePHP bloggers out there, I also received a copy of Cakephp Application Development written by Ahsanul Bari and Anupom Syam, published by PACKT publishing.

The first thing I noticed were the cake’s printed on the cover, they don’t look really tasty to me because of the odd colors. Thankfully the inside of the book looked much better.

Read the full article

GIT: ignoring changes in tracked files

5 years ago

There may be times when you want to edit some variables in for example a database connection file, to run an application right from within your GIT repo. Of course you don’t wont those changes to be commited, so you add the file the .gitignore.
However adding tracked files to .gitignore won’t work because GIT will still track the changes and commit the file if you use the -a parameter.

Fortunately GIT has a very easy solution for this, just run the following command on the file or path you want to ignore the changes of:

git update-index --assume-unchanged <file>

If you wanna start tracking changes again run the following command:

git update-index --no-assume-unchanged <file>

You can find more info about this in the git manual.

Happy GITting ;)

Older Posts »