jquery - How to use .promise( ) on a .on( ) event listener? -
i have .on() listener 'fetch' event. there easy way can fire event when finishes?
this.on('fetch', function() { log('fetch event started!!'); }); // fetch event ended???
i have been playing around .promise() not getting results.
update - here code tapping fetch event collection can show/hide loading icons , other work.
var tweetlist = backbone.collection.extend({ model: tweetmodel, // used collection.sort() comparator: function (item) { return item.get("createddate"); }, initialize: function(models, options) { this.on('fetch', function() { console.log('fetch event started!!'); }); }, fetch: function(options) { this.trigger('fetch', this, options); return backbone.collection.prototype.fetch.call(this, options); } });
i tried binding event fetch doesn't it's firing correctly.
var tweetlist = backbone.collection.extend({ model: tweetmodel, // used collection.sort() comparator: function (item) { return item.get("sortorder"); }, initialize: function(models, options) { this.on('fetch', function(m, o) { log(o.status); }); }, fetch: function(options) { this.trigger('fetch', this, { status: "started"}); this.trigger('fetch', this, { status: "ended"}); return backbone.collection.prototype.fetch.call(this, options); } });
using q (https://github.com/kriskowal/q) , jquery potentially...
this.on('fetch', function() { console.log('fetch event started!!'); q($.ajax(...)).then(function () { console.log("fetch event ended..."); }); });
alternatively, here's example using jquery...
this.on('fetch', function() { console.log('fetch event started!!'); $.when( $.ajax(...) ).then(function( data, textstatus, jqxhr ) { console.log('fetch event ended...'); }); });
Comments
Post a Comment