Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Issue #80. Adding default attributes to Backbone.Model

  • Loading branch information...
commit f3e961da08b1560aa48763302cc5185d290d4659 1 parent d574c9f
@jashkenas authored
Showing with 36 additions and 1 deletion.
  1. +3 −1 backbone.js
  2. +21 −0 index.html
  3. +12 −0 test/model.js
View
4 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 = {});
@timmywil
timmywil added a note

I just went through blame to comment on this style. For readability, you could do

if (!attributes) {
  attributes = {};
}

and both Closure Compiler and UglifyJS will compile it to

attributes||(attributes={});
@jashkenas Owner

Backbone shouldn't depend on any compiler to run efficiently, but thanks.

@timmywil
timmywil added a note

In this case, it can be depended upon. It's pretty simple. Unless you disagree that it is more readable, there's no reason not to change it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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);
View
21 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 @@ <h2 id="Model">Backbone.Model</h2>
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>
View
12 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() {

2 comments on commit f3e961d

@danigb

nice one! thanks!

@collin

<3

@timmywil

I just went through blame to comment on this style. For readability, you could do

if (!attributes) {
  attributes = {};
}

and both Closure Compiler and UglifyJS will compile it to

attributes||(attributes={});
@jashkenas

Backbone shouldn't depend on any compiler to run efficiently, but thanks.

@timmywil

In this case, it can be depended upon. It's pretty simple. Unless you disagree that it is more readable, there's no reason not to change it.

Please sign in to comment.
Something went wrong with that request. Please try again.