Lines Matching full:model
67 // `application/x-www-form-urlencoded` instead and will send the model in a
68 // form param named `model`.
71 // Proxy Backbone class methods to Underscore functions, wrapping the model's
74 // collection.filter(function(model) { return model.get('age') > 10 });
109 if (_.isString(iteratee)) return function(model) { return model.get(iteratee); };
114 return function(model) { argument
115 return matcher(model.attributes);
384 // Backbone.Model
392 // Create a new model with the specified attributes. A client id (`cid`)
394 var Model = Backbone.Model = function(attributes, options) {
409 // Attach all inheritable methods to the Model prototype.
410 _.extend(Model.prototype, Events, {
423 // You may want to override this if you're experiencing name clashes with model ids.
427 // or object. preinitialize will run before any instantiation logic is run in the Model.
434 // Return a copy of the model's `attributes` object.
440 // custom syncing semantics for *this* particular model.
466 // Set a hash of model attributes on the object, firing `"change"`. This is
467 // the core primitive operation of a model, updating the data and notifying
540 // Remove an attribute from the model, firing `"change"`. `unset` is a noop
546 // Clear all attributes on the model, firing `"change"`.
553 // Determine if the model has changed since the last `"change"` event.
564 // You can also pass an attributes object to diff against the model,
587 // Get all of the attributes of the model at the time of the previous
593 // Fetch the model from the server, merging the response with the model's
597 var model = this;
600 var serverAttrs = options.parse ? model.parse(resp, options) : resp;
601 if (!model.set(serverAttrs, options)) return false;
602 if (success) success.call(options.context, model, resp, options);
603 model.trigger('sync', model, resp, options);
609 // Set a hash of model attributes, and sync the model to the server.
610 // If the server returns an attributes hash that differs, the model's
627 // the model will be valid when the attributes, if any, are set.
636 var model = this;
641 model.attributes = attributes;
642 var serverAttrs = options.parse ? model.parse(resp, options) : resp;
644 if (serverAttrs && !model.set(serverAttrs, options)) return false;
645 if (success) success.call(options.context, model, resp, options);
646 model.trigger('sync', model, resp, options);
663 // Destroy this model on the server if it was already persisted.
664 // Optimistically removes the model from its collection, if it has one.
668 var model = this;
673 model.stopListening();
674 model.trigger('destroy', model, model.collection, options);
679 if (success) success.call(options.context, model, resp, options);
680 if (!model.isNew()) model.trigger('sync', model, resp, options);
694 // Default URL for the model's representation on the server -- if you're
708 // the model. The default implementation is just to pass the response along.
713 // Create a new model with identical attributes to this one.
718 // A model is new if it has never been saved to the server, and lacks an id.
723 // Check if the model is currently in a valid state.
728 // Run validation against the next complete set of model attributes,
741 // Underscore methods that we want to implement on the Model, mapped to the
746 // Mix in each Underscore method as a proxy to `Model#attributes`.
747 addUnderscoreMethods(Model, modelMethods, 'attributes');
759 // Create a new **Collection**, perhaps to contain a specific type of `model`.
765 if (options.model) this.model = options.model;
790 // The default model for a collection is just a **Backbone.Model**.
792 model: Model, property
806 return this.map(function(model) { return model.toJSON(options); }); argument
814 // Add a model, or list of models to the set. `models` may be Backbone
821 // Remove a model, or a list of models from the set.
836 // already exist in the collection, as necessary. Similar to **Model#set**,
868 // Turn bare objects into model references, and prevent invalid models
870 var model, i;
872 model = models[i];
875 // optionally merge it into the existing model.
876 var existing = this.get(model);
878 if (merge && model !== existing) {
879 var attrs = this._isModel(model) ? model.attributes : model;
891 // If this is a new, valid model, push it to the `toAdd` list.
893 model = models[i] = this._prepareModel(model, options);
894 if (model) {
895 toAdd.push(model);
896 this._addReference(model, options);
897 modelMap[model.cid] = true;
898 set.push(model);
906 model = this.models[i];
907 if (!modelMap[model.cid]) toRemove.push(model);
935 model = toAdd[i];
936 model.trigger('add', model, this, options);
949 // Return the added (or merged) model (or models).
969 // Add a model to the end of the collection.
970 push: function(model, options) { argument
971 return this.add(model, _.extend({at: this.length}, options));
974 // Remove a model from the end of the collection.
976 var model = this.at(this.length - 1);
977 return this.remove(model, options);
980 // Add a model to the beginning of the collection.
981 unshift: function(model, options) { argument
982 return this.add(model, _.extend({at: 0}, options));
985 // Remove a model from the beginning of the collection.
987 var model = this.at(0);
988 return this.remove(model, options);
996 // Get a model from the set by id, cid, model object with id or cid
1005 // Returns `true` if the model is in the collection.
1010 // Get the model at the given index.
1022 // Return the first model with matching attributes. Useful for simple cases
1049 // Pluck an attribute from each model in the collection.
1071 // Create a new instance of a model in this collection. Add the model to the
1074 create: function(model, options) { argument
1077 model = this._prepareModel(model, options);
1078 if (!model) return false;
1079 if (!wait) this.add(model, options);
1086 model.save(null, options);
1087 return model;
1099 model: this.model, property
1106 return attrs[this.model.prototype.idAttribute || 'id'];
1117 // Prepare a hash of attributes (or other model) to be added to this
1126 var model = new this.model(attrs, options);
1127 if (!model.validationError) return model;
1128 this.trigger('invalid', this, model.validationError, options);
1136 var model = this.get(models[i]);
1137 if (!model) continue;
1139 var index = this.indexOf(model);
1145 delete this._byId[model.cid];
1146 var id = this.modelId(model.attributes);
1151 model.trigger('remove', model, this, options);
1154 removed.push(model);
1155 this._removeReference(model, options);
1160 // Method for checking whether an object should be considered a model for
1162 _isModel: function(model) { argument
1163 return model instanceof Model;
1166 // Internal method to create a model's ties to a collection.
1167 _addReference: function(model, options) { argument
1168 this._byId[model.cid] = model;
1169 var id = this.modelId(model.attributes);
1170 if (id != null) this._byId[id] = model;
1171 model.on('all', this._onModelEvent, this);
1174 // Internal method to sever a model's ties to a collection.
1175 _removeReference: function(model, options) { argument
1176 delete this._byId[model.cid];
1177 var id = this.modelId(model.attributes);
1179 if (this === model.collection) delete model.collection;
1180 model.off('all', this._onModelEvent, this);
1183 // Internal method called every time a model in the set fires an event.
1187 _onModelEvent: function(event, model, collection, options) { argument
1188 if (model) {
1190 if (event === 'destroy') this.remove(model, options);
1192 var prevId = this.modelId(model.previousAttributes());
1193 var id = this.modelId(model.attributes);
1196 if (id != null) this._byId[id] = model;
1245 …var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName', 'event…
1393 // model in question. By default, makes a RESTful Ajax request
1394 // to the model's `url()`. Some possible customizations could be:
1403 // instead of `application/json` with the model in a param named `model`.
1406 Backbone.sync = function(method, model, options) { argument
1420 params.url = _.result(model, 'url') || urlError();
1424 …if (options.data == null && model && (method === 'create' || method === 'update' || method === 'pa…
1426 params.data = JSON.stringify(options.attrs || model.toJSON(options));
1432 params.data = params.data ? {model: params.data} : {};
1462 model.trigger('request', model, xhr, options);
1928 // Set up inheritance for the model, collection, router, view and history.
1929 Model.extend = Collection.extend = Router.extend = View.extend = History.extend = extend;
1937 var wrapError = function(model, options) { argument
1940 if (error) error.call(options.context, model, resp, options);
1941 model.trigger('error', model, resp, options);