nytimes / backbone.trackit

Manage unsaved changes in a Backbone Model.
202 stars 33 forks source link

changes to model not being tracked #5

Closed nikmartin closed 10 years ago

nikmartin commented 10 years ago

I am using Backbone, Marionette, stickit, and just installed trackit to give me PATCH support on model.save. When my view loads, I do:

return Backbone.Marionette.ItemView.extend({
         initialize: function(){
            this.model.startTracking();
         },

and if I set a breakpoint inside startTracking, It gets hit at the proper time properly. Just before calling model.save, I do:

 var changes = this.model.unsavedAttributes();
 this.model.save(changes, {patch:true, wait:true})

but changes is always false, and a full copy of my model is sent with the save.

I set a break point in the Backbone.Model.prototype.set wrapper, and it never gets hit.

Could this be due to a conflict with stickit? Marionette? For some reason my model is getting updated just fine by stickit, but the set function in trackit is never called, leading me to believe something else is overloading Model.set.

delambo commented 10 years ago

I think your problem might be resolved with recent changes on master. Can you try the latest?

nikmartin commented 10 years ago

I will, I had pulled trackit out when it didn't seem to be tracking changes for me, but I'll out it back in and give it a try.

nikmartin commented 10 years ago

I must be doing something TOTALLY wrong. I'm using this with stickit, and call this.stickit() at the end of the render function as usual, and if I change several model attributes, then hit my save function, console.log(view.model.unsavedAttributes()); returns false.

I'm also using marionette if that matters, along with backbone.deep-model.

nikmartin commented 10 years ago

Backbone.Model.prototype.set = _.wrap(Backbone.Model.prototype.set, function(oldSet, key, val, options) {

this is not firing when used with stickit and deep-model. When I type into an html field, this doesnt work.

nikmartin commented 10 years ago

Ok, I got it to work in the WORST way possible:

at https://github.com/NYTimes/backbone.trackit/blob/master/backbone.trackit.js#L146

I did: Backbone.DeepModel.prototype.set = _.wrap(Backbone.Model.prototype.set || Backbone.DeepModel.prototype.set, function(oldSet, key, val, options) {

I don't know another way to handle loading a plugin like backbone.trackit, when another library has already monkeypatched Backbone.Model, like Backbone.DeepModel