Skip to content

Commit

Permalink
Media.List: no pisar 'collection' en el success de ajax.
Browse files Browse the repository at this point in the history
Es una solución temporal a #2.

Signed-off-by: Adrian Pardini <github@tangopardo.com.ar>
  • Loading branch information
pardo-bsso committed Mar 24, 2013
1 parent 62d916b commit 4158a1c
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion models/Media.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ Media.Model = Backbone.Model.extend({
}
},
defaults: {
_id: null,
_id: '',
stat: {},
file: "None",
name: "",
audio: "None",
Expand Down Expand Up @@ -184,6 +185,64 @@ Media.List = Media.Model.extend ({
duration: 0,
pos: 0,
},

// Set a hash of model attributes, and sync the model to the server.
// If the server returns an attributes hash that differs, the model's
// state will be `set` again.
save: function(key, val, options) {
var attrs, success, method, xhr, attributes = this.attributes;

// Handle both `"key", value` and `{key: value}` -style arguments.
if (key == null || typeof key === 'object') {
attrs = key;
options = val;
} else {
(attrs = {})[key] = val;
}

//XXX:
// If we're not waiting and attributes exist, save acts as `set(attr).save(null, opts)`.
if (attrs && (!options || !options.wait) && !this.set(attrs, options)) return false;

options = _.extend({validate: true}, options);

// Do not persist invalid models.
if (!this._validate(attrs, options)) return false;

// Set temporary attributes if `{wait: true}`.
if (attrs && options.wait) {
this.attributes = _.extend({}, attributes, attrs);
}

// After a successful server-side save, the client is (optionally)
// updated with the server-side state.
if (options.parse === void 0) options.parse = true;
success = options.success;
options.success = function(model, resp, options) {
// Ensure attributes are restored during synchronous saves.
model.attributes = attributes;
var serverAttrs = model.parse(resp, options);
//XXX:
delete serverAttrs['collection'];
if (options.wait) serverAttrs = _.extend(attrs || {}, serverAttrs);
if (_.isObject(serverAttrs) && !model.set(serverAttrs, options)) {
return false;
}
if (success) success(model, resp, options);
};

// Finish configuring and sending the Ajax request.
method = this.isNew() ? 'create' : (options.patch ? 'patch' : 'update');
if (method === 'patch') options.attrs = attrs;
xhr = this.sync(method, this, options);

// Restore attributes.
if (attrs && options.wait) this.attributes = attributes;

return xhr;
},


});

Media.Universe = Media.Collection.extend ({
Expand Down

0 comments on commit 4158a1c

Please sign in to comment.