[Pkg-javascript-commits] [backbone] 09/15: Issue #80. Adding default attributes to Backbone.Model
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 16:59:30 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.3.2
in repository backbone.
commit f3e961da08b1560aa48763302cc5185d290d4659
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Mon Nov 22 14:22:21 2010 -0500
Issue #80. Adding default attributes to Backbone.Model
---
backbone.js | 4 +++-
index.html | 21 +++++++++++++++++++++
test/model.js | 12 ++++++++++++
3 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/backbone.js b/backbone.js
index bf9439b..561e6bb 100644
--- a/backbone.js
+++ b/backbone.js
@@ -113,9 +113,11 @@
// Create a new model, with defined attributes. A client id (`cid`)
// is automatically generated and assigned for you.
Backbone.Model = function(attributes, options) {
+ attributes || (attributes = {});
+ if (this.defaults) attributes = _.extend({}, this.defaults, attributes);
this.attributes = {};
this.cid = _.uniqueId('c');
- this.set(attributes || {}, {silent : true});
+ this.set(attributes, {silent : true});
this._previousAttributes = _.clone(this.attributes);
if (options && options.collection) this.collection = options.collection;
this.initialize(attributes, options);
diff --git a/index.html b/index.html
index 5ed826c..c2ed729 100644
--- a/index.html
+++ b/index.html
@@ -173,6 +173,7 @@
<li>– <a href="#Model-id">id</a></li>
<li>– <a href="#Model-cid">cid</a></li>
<li>– <a href="#Model-attributes">attributes</a></li>
+ <li>– <a href="#Model-defaults">defaults</a></li>
<li>- <a href="#Model-toJSON">toJSON</a></li>
<li>– <a href="#Model-fetch">fetch</a></li>
<li>– <a href="#Model-save">save</a></li>
@@ -613,6 +614,26 @@ note.set({title: "October 12", content: "Lorem Ipsum Dolor Sit Amet..."});
them directly. If you'd like to retrieve and munge a copy of the model's
attributes, use <a href="#Model-toJSON">toJSON</a> instead.
</p>
+
+ <p id="Model-defaults">
+ <b class="header">defaults</b><code>model.defaults</code>
+ <br />
+ The <b>defaults</b> hash can be used to specify the default attributes
+ for your model. When creating an instance of the model, any unspecified
+ attributes will be set to their default value.
+ </p>
+
+<pre class="runnable">
+var Meal = Backbone.Model.extend({
+ defaults: {
+ "appetizer": "caesar salad",
+ "entree": "ravioli",
+ "dessert": "cheesecake"
+ }
+});
+
+alert("Dessert will be " + (new Meal).get('dessert'));
+</pre>
<p id="Model-toJSON">
<b class="header">toJSON</b><code>model.toJSON()</code>
diff --git a/test/model.js b/test/model.js
index 3622015..8894987 100644
--- a/test/model.js
+++ b/test/model.js
@@ -126,6 +126,18 @@ $(document).ready(function() {
equals(model.get('name'), undefined);
});
+ test("Model: defaults", function() {
+ var Defaulted = Backbone.Model.extend({
+ defaults: {
+ "one": 1,
+ "two": 2
+ }
+ });
+ var model = new Defaulted({two: null});
+ equals(model.get('one'), 1);
+ equals(model.get('two'), null);
+ });
+
test("Model: changed, hasChanged, changedAttributes, previous, previousAttributes", function() {
var model = new Backbone.Model({name : "Tim", age : 10});
model.bind('change', function() {
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git
More information about the Pkg-javascript-commits
mailing list