Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development to Master #5

Merged
merged 4 commits into from
Jul 2, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 59 additions & 9 deletions backbone.nest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@
//then recursively call toJSON on models within.
toJSON: function() {
var attr = this.attributes,
resp = _.clone(attr);
resp = _.clone(attr),
nests = this.nests.split(" "),
i, j;
//Optimize if we know there is only one nested
//collection and we know the name of it.
if(this.nest) {
resp[this.nest] = resp[this.nest].toJSON();
if(nests.length === 1) {
resp[this.nests] = resp[this.nests].toJSON();
return resp;
}
} else
//Optimize if there are multiple and we know the names.
if(this.nests) {
var i, j, nests = this.nests.split(" ");
for(i = 0, j = nests.length; i < j; i++) {
resp[nests[i]] = resp[nests[i]].toJSON();
}
return resp;
}
//If not loop through all.
for(prop in attr) {
Expand All @@ -32,19 +34,45 @@
};
return resp;
},
//parse
//-----
//This creates a collection if model is passed an array
//under the nest attributes name
parse: function(data) {
var i, j, nests = this.nests.split(" ");
if(nests.length === 1) {
data[this.nests] = new this.nest(data[this.nests]);
} else
if(this.nests) {
for(i = 0, j = nests.length; i < j; i++) {
data[nests[i]] = new this.nest(data[nests[i]]);
}
}
return data;
},
//listenToCollection
//------------------
//This bubbles change events from nested collections
//up to the container model, not auto to optimize for
//standard models.
listenToCollection: function() {
var i, j, nests = this.nests.split(" ");
var attr = this.attributes;
//Optimize if nested collection is explictly known.
if(this.nest) {
return this.listenTo(attr[this.nest], "change", function() {
this.trigger("change change:"+this.nest);
if(nests.length === 1) {
return this.listenTo(attr[this.nests], "change", function() {
this.trigger("change change:"+this.nests);
}, this);
};
} else
//Optimize for multiple and known.
if(this.nests) {
for(i = 0, j = nests.length; i < j; i++) {
this.listenTo(attr[nests[i]], "change", function() {
this.trigger("change change:"+nests[i]);
}, this);
}
return;
}
//If not loop through all.
for(prop in attr) {
if(attr[prop] instanceof Backbone.Collection) {
Expand All @@ -55,4 +83,26 @@
};
}
});
//Proxy collection methods on nest via the model.
//-----------------------------------------------

//List all methods we want to proxy
var methods = ['push', 'pop', 'unshift', 'shift'];

//Go through each one, have it invoke upon the nest
_.each(methods, function(method) {
Backbone.Model.prototype[method] = function() {
var args = Array.prototype.slice.call(arguments),
nests = this.nests.split(" "), i, j;
if(nest.length === 1) {
return Backbone.Collection.prototype[method].apply(this.attributes[this.nests], args);
}
return function(){
for(i = 0, j = nests.length; i < j; i++){
Backbone.Collection.prototype[method].apply(this.attributes[nests[i]], args);
};
};
};
});

}(Backbone))
174 changes: 154 additions & 20 deletions docs/backbone.nest.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ <h2>toJSON</h2>

</div>

<div class="content"><div class='highlight'><pre> toJSON: <span class="keyword">function</span>(collection) {
<div class="content"><div class='highlight'><pre> toJSON: <span class="keyword">function</span>() {
<span class="keyword">var</span> attr = <span class="keyword">this</span>.attributes,
resp = _.clone(attr);</pre></div></div>
resp = _.clone(attr),
nests = <span class="keyword">this</span>.nests.split(<span class="string">" "</span>),
i, j;</pre></div></div>

</li>

Expand All @@ -106,10 +108,10 @@ <h2>toJSON</h2>

</div>

<div class="content"><div class='highlight'><pre> <span class="keyword">if</span>(collection) {
resp[collection] = resp[collection].toJSON();
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span>(nests.length === <span class="number">1</span>) {
resp[<span class="keyword">this</span>.nests] = resp[<span class="keyword">this</span>.nests].toJSON();
<span class="keyword">return</span> resp;
}</pre></div></div>
} <span class="keyword">else</span></pre></div></div>

</li>

Expand All @@ -120,6 +122,26 @@ <h2>toJSON</h2>
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">&#182;</a>
</div>
<p>Optimize if there are multiple and we know the names.</p>

</div>

<div class="content"><div class='highlight'><pre> <span class="keyword">if</span>(<span class="keyword">this</span>.nests) {
<span class="keyword">for</span>(i = <span class="number">0</span>, j = nests.length; i &lt; j; i++) {
resp[nests[i]] = resp[nests[i]].toJSON();
}
<span class="keyword">return</span> resp;
}</pre></div></div>

</li>


<li id="section-8">
<div class="annotation">

<div class="pilwrap ">
<a class="pilcrow" href="#section-8">&#182;</a>
</div>
<p>If not loop through all.</p>

</div>
Expand All @@ -135,11 +157,51 @@ <h2>toJSON</h2>
</li>


<li id="section-8">
<li id="section-9">
<div class="annotation">

<div class="pilwrap for-h2">
<a class="pilcrow" href="#section-8">&#182;</a>
<a class="pilcrow" href="#section-9">&#182;</a>
</div>
<h2>parse</h2>

</div>

</li>


<li id="section-10">
<div class="annotation">

<div class="pilwrap ">
<a class="pilcrow" href="#section-10">&#182;</a>
</div>
<p>This creates a collection if model is passed an array
under the nest attributes name</p>

</div>

<div class="content"><div class='highlight'><pre> parse: <span class="keyword">function</span>(data) {
<span class="keyword">var</span> i, j, nests = <span class="keyword">this</span>.nests.split(<span class="string">" "</span>);
<span class="keyword">if</span>(nests.length === <span class="number">1</span>) {
data[<span class="keyword">this</span>.nests] = <span class="keyword">new</span> <span class="keyword">this</span>.nest(data[<span class="keyword">this</span>.nests]);
} <span class="keyword">else</span>
<span class="keyword">if</span>(<span class="keyword">this</span>.nests) {
<span class="keyword">for</span>(i = <span class="number">0</span>, j = nests.length; i &lt; j; i++) {
data[nests[i]] = <span class="keyword">new</span> <span class="keyword">this</span>.nest(data[nests[i]]);
}
}
<span class="keyword">return</span> data;
},</pre></div></div>

</li>


<li id="section-11">
<div class="annotation">

<div class="pilwrap for-h2">
<a class="pilcrow" href="#section-11">&#182;</a>
</div>
<h2>listenToCollection</h2>

Expand All @@ -148,48 +210,71 @@ <h2>listenToCollection</h2>
</li>


<li id="section-9">
<li id="section-12">
<div class="annotation">

<div class="pilwrap ">
<a class="pilcrow" href="#section-9">&#182;</a>
<a class="pilcrow" href="#section-12">&#182;</a>
</div>
<p>This bubbles change events from nested collections
up to the container model, not auto to optimize for
standard models. </p>

</div>

<div class="content"><div class='highlight'><pre> listenToCollection: <span class="keyword">function</span>(prop) {
<div class="content"><div class='highlight'><pre> listenToCollection: <span class="keyword">function</span>() {
<span class="keyword">var</span> i, j, nests = <span class="keyword">this</span>.nests.split(<span class="string">" "</span>);
<span class="keyword">var</span> attr = <span class="keyword">this</span>.attributes;</pre></div></div>

</li>


<li id="section-10">
<li id="section-13">
<div class="annotation">

<div class="pilwrap ">
<a class="pilcrow" href="#section-10">&#182;</a>
<a class="pilcrow" href="#section-13">&#182;</a>
</div>
<p>Optimize if nested collection is explictly named.</p>
<p>Optimize if nested collection is explictly known.</p>

</div>

<div class="content"><div class='highlight'><pre> <span class="keyword">if</span>(prop) {
<span class="keyword">return</span> <span class="keyword">this</span>.listenTo(attr[prop], <span class="string">"change"</span>, <span class="keyword">function</span>() {
<span class="keyword">this</span>.trigger(<span class="string">"change change:"</span>+prop);
<div class="content"><div class='highlight'><pre> <span class="keyword">if</span>(nests.length === <span class="number">1</span>) {
<span class="keyword">return</span> <span class="keyword">this</span>.listenTo(attr[<span class="keyword">this</span>.nests], <span class="string">"change"</span>, <span class="keyword">function</span>() {
<span class="keyword">this</span>.trigger(<span class="string">"change change:"</span>+<span class="keyword">this</span>.nests);
}, <span class="keyword">this</span>);
};</pre></div></div>
} <span class="keyword">else</span></pre></div></div>

</li>


<li id="section-11">
<li id="section-14">
<div class="annotation">

<div class="pilwrap ">
<a class="pilcrow" href="#section-11">&#182;</a>
<a class="pilcrow" href="#section-14">&#182;</a>
</div>
<p>Optimize for multiple and known.</p>

</div>

<div class="content"><div class='highlight'><pre> <span class="keyword">if</span>(<span class="keyword">this</span>.nests) {
<span class="keyword">for</span>(i = <span class="number">0</span>, j = nests.length; i &lt; j; i++) {
<span class="keyword">this</span>.listenTo(attr[nests[i]], <span class="string">"change"</span>, <span class="keyword">function</span>() {
<span class="keyword">this</span>.trigger(<span class="string">"change change:"</span>+nests[i]);
}, <span class="keyword">this</span>);
}
<span class="keyword">return</span>;
}</pre></div></div>

</li>


<li id="section-15">
<div class="annotation">

<div class="pilwrap ">
<a class="pilcrow" href="#section-15">&#182;</a>
</div>
<p>If not loop through all.</p>

Expand All @@ -203,7 +288,56 @@ <h2>listenToCollection</h2>
};
};
}
});
});</pre></div></div>

</li>


<li id="section-16">
<div class="annotation">

<div class="pilwrap for-h2">
<a class="pilcrow" href="#section-16">&#182;</a>
</div>
<h2>Proxy collection methods on nest via the model.</h2>

</div>

</li>


<li id="section-17">
<div class="annotation">

<div class="pilwrap ">
<a class="pilcrow" href="#section-17">&#182;</a>
</div>
<p>List all methods we want to proxy</p>

</div>

<div class="content"><div class='highlight'><pre> <span class="keyword">var</span> methods = [<span class="string">'push'</span>, <span class="string">'pop'</span>, <span class="string">'unshift'</span>, <span class="string">'shift'</span>];</pre></div></div>

</li>


<li id="section-18">
<div class="annotation">

<div class="pilwrap ">
<a class="pilcrow" href="#section-18">&#182;</a>
</div>
<p>Go through each one, have it invoke upon the nest</p>

</div>

<div class="content"><div class='highlight'><pre> _.each(methods, <span class="keyword">function</span>(method) {
Backbone.Model.prototype[method] = <span class="keyword">function</span>() {
<span class="keyword">var</span> args = Array.prototype.slice.call(arguments);
<span class="keyword">return</span> Backbone.Collection.prototype[method].apply(<span class="keyword">this</span>.attributes[<span class="keyword">this</span>.nest], args);
}
});

}(Backbone))</pre></div></div>

</li>
Expand Down
20 changes: 12 additions & 8 deletions tests/console.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

var TestCollection = new Backbone.Collection([
var testcollection = new Backbone.Collection([
{
name:"test1",
type:"model"
Expand All @@ -9,11 +9,13 @@ var TestCollection = new Backbone.Collection([
type:"model"
}
]),
TestModel = new Backbone.Model({
name:"master 1",
type:"master model",
test:TestCollection
});
TestModel = Backbone.Model.extend({
nest: "Coll"
}),
testmodel = new TestModel({
Coll: testcollection,
name:"base"
})

TestModel.on("change", function (change) {
console.log("TestModel::change:");
Expand All @@ -22,5 +24,7 @@ TestCollection.on("change", function (change) {
console.log("TestCollection::change")
});

var mod = TestModel,
col = TestCollection;
var mod = testmodel,
col = testcollection;