[Pkg-javascript-commits] [backbone] 65/101: down at the bottom of the model docco
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 16:58:29 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.1.0
in repository backbone.
commit 66fb51ca36881d58a873e6eadc25db4b23cf9c5e
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Tue Oct 12 11:47:57 2010 -0400
down at the bottom of the model docco
---
backbone.js | 22 +++++++++---------
docs/backbone.html | 22 +++++++++---------
index.html | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
test/model.js | 6 ++---
4 files changed, 91 insertions(+), 27 deletions(-)
diff --git a/backbone.js b/backbone.js
index c52318a..2a8316f 100644
--- a/backbone.js
+++ b/backbone.js
@@ -115,7 +115,7 @@
this._attributes = {};
this.cid = _.uniqueId('c');
this.set(attributes || {}, {silent : true});
- this._formerAttributes = this.attributes();
+ this._previousAttributes = this.attributes();
};
// `attributes` is aliased as `toJSON`, for use with `JSON.stringify`.
@@ -128,7 +128,7 @@
// A snapshot of the model's previous attributes, taken immediately
// after the last `changed` event was fired.
- _formerAttributes : null,
+ _previousAttributes : null,
// Has the item been changed since the last `changed` event?
_changed : false,
@@ -226,14 +226,14 @@
// Calling this will cause all objects observing the model to update.
change : function() {
this.trigger('change', this);
- this._formerAttributes = this.attributes();
+ this._previousAttributes = this.attributes();
this._changed = false;
},
// Determine if the model has changed since the last `changed` event.
// If you specify an attribute name, determine if that attribute has changed.
hasChanged : function(attr) {
- if (attr) return this._formerAttributes[attr] != this._attributes[attr];
+ if (attr) return this._previousAttributes[attr] != this._attributes[attr];
return this._changed;
},
@@ -242,7 +242,7 @@
// view need to be updated and/or what attributes need to be persisted to
// the server.
changedAttributes : function(now) {
- var old = this.formerAttributes(), now = now || this.attributes(), changed = false;
+ var old = this._previousAttributes, now = now || this.attributes(), changed = false;
for (var attr in now) {
if (!_.isEqual(old[attr], now[attr])) {
changed = changed || {};
@@ -254,15 +254,15 @@
// Get the previous value of an attribute, recorded at the time the last
// `changed` event was fired.
- formerValue : function(attr) {
- if (!attr || !this._formerAttributes) return null;
- return this._formerAttributes[attr];
+ previous : function(attr) {
+ if (!attr || !this._previousAttributes) return null;
+ return this._previousAttributes[attr];
},
// Get all of the attributes of the model at the time of the previous
// `changed` event.
- formerAttributes : function() {
- return this._formerAttributes;
+ previousAttributes : function() {
+ return _.clone(this._previousAttributes);
},
// Set a hash of model attributes, and sync the model to the server.
@@ -467,7 +467,7 @@
switch (ev) {
case 'change':
if (model.hasChanged('id')) {
- delete this._byId[model.formerValue('id')];
+ delete this._byId[model.previous('id')];
this._byId[model.id] = model;
}
this.trigger('change', model);
diff --git a/docs/backbone.html b/docs/backbone.html
index ad805a2..b5bd55e 100644
--- a/docs/backbone.html
+++ b/docs/backbone.html
@@ -73,9 +73,9 @@ If you do not specify the id, a negative id will be assigned for you.</p>
<span class="k">this</span><span class="p">.</span><span class="nx">_attributes</span> <span class="o">=</span> <span class="p">{};</span>
<span class="k">this</span><span class="p">.</span><span class="nx">cid</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">uniqueId</span><span class="p">(</span><span class="s1">'c'</span><span class="p">);</span>
<span class="k">this</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">attributes</span> <span class="o">||</span> <span class="p">{},</span> <span class="p">{</span><span class="nx">silent</span> <span class="o">:</span> <span class="kc">true</span><span class="p">});</span>
- <span class="k">this</span><span class="p">.</span><span class="nx">_formerAttributes</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">attributes</span><span class="p">();</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">_previousAttributes</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">attributes</span><span class="p">();</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">¶</a> </div> <p>Attach all inheritable methods to the Model prototype.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</spa [...]
-after the last <code>changed</code> event was fired.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_formerAttributes</span> <span class="o">:</span> <span class="kc">null</span><span class="p">,</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16"> [...]
+after the last <code>changed</code> event was fired.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_previousAttributes</span> <span class="o">:</span> <span class="kc">null</span><span class="p">,</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&# [...]
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_attributes</span><span class="p">);</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">¶</a> </div> <p>Default URL for the model's representation on the server -- if you're
using Backbone's restful methods, override this to change the endpoint
@@ -131,17 +131,17 @@ silence it.</p> </td> <td class="code"> <d
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">¶</a> </div> <p>Call this method to fire manually fire a <code>change</code> event for this model.
Calling this will cause all objects observing the model to update.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">change</span> <span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">'change'</span><span class="p">,</span> <span class="k">this</span><span class="p">);</span>
- <span class="k">this</span><span class="p">.</span><span class="nx">_formerAttributes</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">attributes</span><span class="p">();</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">_previousAttributes</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">attributes</span><span class="p">();</span>
<span class="k">this</span><span class="p">.</span><span class="nx">_changed</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">¶</a> </div> <p>Determine if the model has changed since the last <code>changed</code> event.
If you specify an attribute name, determine if that attribute has changed.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">hasChanged</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">attr</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">attr</span><span class="p">)</span> <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_formerAttributes</span><span class="p">[</span><span class="nx">attr</span><span class="p">]</span> <span class="o">!=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_attributes</span><span class="p">[</span><span class="nx">attr</span><span class="p">];</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">attr</span><span class="p">)</span> <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_previousAttributes</span><span class="p">[</span><span class="nx">attr</span><span class="p">]</span> <span class="o">!=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_attributes</span><span class="p">[</span><span class="nx">attr</span><span class="p">];</span>
<span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_changed</span><span class="p">;</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">¶</a> </div> <p>Return an object containing all the attributes that have changed, or false
if there are no changed attributes. Useful for determining what parts of a
view need to be updated and/or what attributes need to be persisted to
the server.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">changedAttributes</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">now</span><span class="p">)</span> <span class="p">{</span>
- <span class="kd">var</span> <span class="nx">old</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">formerAttributes</span><span class="p">(),</span> <span class="nx">now</span> <span class="o">=</span> <span class="nx">now</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">attributes</span><span class="p">(),</span> <span class="nx">changed</span> <span class="o">=</span> <span class [...]
+ <span class="kd">var</span> <span class="nx">old</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">previousAttributes</span><span class="p">(),</span> <span class="nx">now</span> <span class="o">=</span> <span class="nx">now</span> <span class="o">||</span> <span class="k">this</span><span class="p">.</span><span class="nx">attributes</span><span class="p">(),</span> <span class="nx">changed</span> <span class="o">=</span> <span cla [...]
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">attr</span> <span class="k">in</span> <span class="nx">now</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">_</span><span class="p">.</span><span class="nx">isEqual</span><span class="p">(</span><span class="nx">old</span><span class="p">[</span><span class="nx">attr</span><span class="p">],</span> <span class="nx">now</span><span class="p">[</span><span class="nx">attr</span><span class="p">]))</span> <span class="p">{</span>
<span class="nx">changed</span> <span class="o">=</span> <span class="nx">changed</span> <span class="o">||</span> <span class="p">{};</span>
@@ -150,12 +150,12 @@ the server.</p> </td> <td class="code"> <d
<span class="p">}</span>
<span class="k">return</span> <span class="nx">changed</span><span class="p">;</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-34">¶</a> </div> <p>Get the previous value of an attribute, recorded at the time the last
-<code>changed</code> event was fired.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">formerValue</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">attr</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">attr</span> <span class="o">||</span> <span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">_formerAttributes</span><span class="p">)</span> <span class="k">return</span> <span class="kc">null</span><span class="p">;</span>
- <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_formerAttributes</span><span class="p">[</span><span class="nx">attr</span><span class="p">];</span>
+<code>changed</code> event was fired.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">previous</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">attr</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">attr</span> <span class="o">||</span> <span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">_previousAttributes</span><span class="p">)</span> <span class="k">return</span> <span class="kc">null</span><span class="p">;</span>
+ <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_previousAttributes</span><span class="p">[</span><span class="nx">attr</span><span class="p">];</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-35"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-35">¶</a> </div> <p>Get all of the attributes of the model at the time of the previous
-<code>changed</code> event.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">formerAttributes</span> <span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
- <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_formerAttributes</span><span class="p">;</span>
+<code>changed</code> event.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">previousAttributes</span> <span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_previousAttributes</span><span class="p">;</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">¶</a> </div> <p>Set a hash of model attributes, and sync the model to the server.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">save</span> <sp [...]
<span class="nx">attrs</span> <span class="o">||</span> <span class="p">(</span><span class="nx">attrs</span> <span class="o">=</span> <span class="p">{});</span>
<span class="nx">options</span> <span class="o">||</span> <span class="p">(</span><span class="nx">options</span> <span class="o">=</span> <span class="p">{});</span>
@@ -294,7 +294,7 @@ Sets need to update their indexes when models change ids.</p> </td>
<span class="k">switch</span> <span class="p">(</span><span class="nx">ev</span><span class="p">)</span> <span class="p">{</span>
<span class="k">case</span> <span class="s1">'change'</span><span class="o">:</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">model</span><span class="p">.</span><span class="nx">hasChanged</span><span class="p">(</span><span class="s1">'id'</span><span class="p">))</span> <span class="p">{</span>
- <span class="k">delete</span> <span class="k">this</span><span class="p">.</span><span class="nx">_byId</span><span class="p">[</span><span class="nx">model</span><span class="p">.</span><span class="nx">formerValue</span><span class="p">(</span><span class="s1">'id'</span><span class="p">)];</span>
+ <span class="k">delete</span> <span class="k">this</span><span class="p">.</span><span class="nx">_byId</span><span class="p">[</span><span class="nx">model</span><span class="p">.</span><span class="nx">previous</span><span class="p">(</span><span class="s1">'id'</span><span class="p">)];</span>
<span class="k">this</span><span class="p">.</span><span class="nx">_byId</span><span class="p">[</span><span class="nx">model</span><span class="p">.</span><span class="nx">id</span><span class="p">]</span> <span class="o">=</span> <span class="nx">model</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">this</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">'change'</span><span class="p">,</span> <span class="nx">model</span><span class="p">);</span>
diff --git a/index.html b/index.html
index 42c0a6b..9e4ae01 100644
--- a/index.html
+++ b/index.html
@@ -154,8 +154,8 @@
<li>– <a href="#Model-change">change</a></li>
<li>– <a href="#Model-hasChanged">hasChanged</a></li>
<li>– <a href="#Model-changedAttributes">changedAttributes</a></li>
- <li>– <a href="#Model-formerValue">formerValue</a></li>
- <li>– <a href="#Model-formerAttributes">formerAttributes</a></li>
+ <li>– <a href="#Model-previous">previous</a></li>
+ <li>– <a href="#Model-previousAttributes">previousAttributes</a></li>
</ul>
<a class="toc_title" href="#Collection">
Collection
@@ -531,7 +531,71 @@ one.set({
Create a new instance of a model with identical attributes.
</p>
+ <p id="Model-isNew">
+ <b class="header">isNew</b><code>model.isNew()</code>
+ <br />
+ Has this model been saved to the server yet? If the model does not yet have
+ an <tt>id</tt>, it is considered to be new.
+ </p>
+
+ <p id="Model-change">
+ <b class="header">change</b><code>model.change()</code>
+ <br />
+ If you've been passing <tt>{silent: true}</tt> to <tt>set</tt> in order to
+ aggregate rapid changes to a model, you'll want to fire the <tt>"change"</tt>
+ event when you're finished. Call <tt>model.change()</tt> to trigger it.
+ </p>
+
+ <p id="Model-hasChanged">
+ <b class="header">hasChanged</b><code>model.hasChanged([attribute])</code>
+ <br />
+ Has the model changed since the last <tt>"change"</tt> event? If an <b>attribute</b>
+ is passed, returns <tt>true</tt> if that specific attribute has changed.
+ </p>
+
+<pre>
+book.bind("change", function() {
+ if (book.hasChanged("title")) {
+ ...
+ }
+});
+</pre>
+ <p id="Model-changedAttributes">
+ <b class="header">changedAttributes</b><code>model.changedAttributes([attributes])</code>
+ <br />
+ Retrieve a hash of only the model's attributes that have changed. Optionally,
+ an external <b>attributes</b> hash can be passed in, returning
+ the attributes in that hash which differ from the model. This can be used
+ to figure out which portions of a view should be updated, or what calls
+ need to be made to sync the changes to the server.
+ </p>
+
+ <p id="Model-previous">
+ <b class="header">previous</b><code>model.previous(attribute)</code>
+ <br />
+ During a <tt>"change"</tt> event, this method can be used to get the
+ previous value of a changed attribute.
+ </p>
+
+<pre class="runnable">
+var bill = new Backbone.Model({
+ name: "Bill Smith"
+});
+
+bill.bind("change:name", function(model, name) {
+ alert("Changed name from " + bill.previous("name") + " to " + name);
+});
+
+bill.set({name : "Bill Jones"});
+</pre>
+
+ <p id="Model-previousAttributes">
+ <b class="header">previousAttributes</b><code>model.previousAttributes()</code>
+ <br />
+ Return a copy of the model's previous attributes. Useful for getting a
+ diff between versions of a model, or for recovering from a failed validation.
+ </p>
diff --git a/test/model.js b/test/model.js
index 01f7d60..7f77ad8 100644
--- a/test/model.js
+++ b/test/model.js
@@ -84,14 +84,14 @@ $(document).ready(function() {
ok(changeCount == 2, "Change count should have incremented for unset.");
});
- test("model: changed, hasChanged, changedAttributes, formerValue, formerAttributes", function() {
+ test("model: changed, hasChanged, changedAttributes, previous, previousAttributes", function() {
var model = new Backbone.Model({name : "Tim", age : 10});
model.bind('change', function() {
ok(model.hasChanged('name'), 'name changed');
ok(!model.hasChanged('age'), 'age did not');
ok(_.isEqual(model.changedAttributes(), {name : 'Rob'}), 'changedAttributes returns the changed attrs');
- equals(model.formerValue('name'), 'Tim');
- ok(_.isEqual(model.formerAttributes(), {name : "Tim", age : 10}), 'formerAttributes is correct');
+ equals(model.previous('name'), 'Tim');
+ ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previousAttributes is correct');
});
model.set({name : 'Rob'}, {silent : true});
model.change();
--
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