javascript - How and when to use .on or Events : {} with backbone -
javascript - How and when to use .on or Events : {} with backbone -
i building web app using backbone , im maintain running in problem events not bind according objects.
i using backbone in combination qunit , sinon testing. want grab connection errors if beingness made. understanding of backbone collection whenever collection.create()
called backbone save new model server through collection "request"
, "sync"
or perchance "error"
events should called. in collection have added:
var cproduct = backbone.collection.extend({ // code events : { 'sync' : 'success', 'error' : 'fail' }, success : function() { console.log('success'); }, fail: function() { console.log('sync error'); }, // more code });
at other point in code triggered button click:
createproduct { alert('creating model'); var title = $('#title').val(); var description = $('#description').val(); this.collection.create({'title':title,'description':description}); }
where alert check if particular event click fired , collection cproduct collection. note in view containing collection.
i utilize sinon false server phone call , repley next code:
module("product models",{ setup: function(){ server = sinon.fakeserver.create(); }, teardown: function(){} } asynctest("sometest", 1 , function(){ //some preparation code var test = function(eventdata) { console.log(eventdata); server.respond(); }; server.respondwith("get", "/api/testmodel/1",[500, { "content-type": "application/json"}, json.stringify({"fail":"error"})]); //trying create fail cproduct.on('request', test); new expandproductfrom({'collection':cproduct}); }
but after neither of events triggered. after having taken @ backbone api found .on
binding events objects. replaced events : {}
:
initialize : function() { this.on('sync',this.success); this.on('error',this.fail); }
this made success function run if corrected respondwith
function.
to comeback question: how utilize events:{}
or .on
function?
thanks
the short reply is...
events:{}
this property capture delegate events happening in dom(templates).
e.g., button click event
on()/bind()
these methods capture internal events happening within backbone
eg., collection/model add together event
javascript backbone.js qunit sinon
Comments
Post a Comment