Backbone.js "create" callback?

I have a very large stream that goes through loading / rendering a collection, adding a new model to this collection, synchronizing this new model with the database via the API, and then re-rendering my collection ... but I am facing one problem that I am I can not do:

Is there a standard "Backbone" way to insert a callback into the "create / sync to backend -> success" event, or do I just need to overwrite the create function? I could use the add method (which is processed every time a new model is added to the collection / rendering on dom), but then it fires when each model is filled when the page loads ... and I really want it only when the user creates / inserts new model to the collection after the page is already displayed.

EDIT: If anyone else has this problem, back and forth with Elf below led me to the following solution:

Just add something along the lines coll.trigger("newModelCreated", nextModel);to the success callback in Backbone.collection.create - and then bind the event to this trigger in the view associated with this collection (so, in my opinion, the initialization function this.collection.bind('newModelCreated', this.createAndAdd, this);.

Then, when this trigger is fired, I simply redirect it to a special version of my “addOne” function (which I named createAndAdd), which displays my new model + additional DOM material that I want to associate with the newly created one.

Not sure what the optimal solution is, but it works for me.

+5
source share
2 answers

() ? ? , , .

, URL- . , POST " ", , . add(). (, - , collection.add, , , .)

+2

backbone.sync mongodb. fetch script, , , - . Backbone fetchComplete , . script , ( unbind) fetchComplete.

0

All Articles