I like backbone.js more and more. I want to have several views for this model:
- A view list in which each model has a view with its own element li.
- Detailed view showing all the details of the model.
My question is: I was looking for a good way to allow one species to communicate with another and chose the following:
Backbone.Model.prototype.addView = function (view) {
if (typeof this.views === 'undefined')
{
this.views = [];
}
if (_.indexOf(this.views, view) === -1)
{
this.views.push(view);
}
}
Backbone.Model.prototype.unloadViews = function (args) {
var n = (typeof this.views === 'undefined') ? 0 : this.views.length;
for (var i = 0; i < n; i++)
{
var view = this.views[i];
if (typeof view.unloadView === 'function')
{
view.unloadView(args);
}
}
}
Backbone.Model.prototype.renderViews = function (args) {
var n = (typeof this.views === 'undefined') ? 0 : this.views.length;
for (var i = 0; i < n; i++)
{
var view = this.views[i];
if (typeof view.render === 'function')
{
view.render(args);
}
}
}
My questions
- Am I missing something when I study backbone.js, which will let me do this nativly?
- Is there a reason I should avoid this?
Additional Information
( ) GitHub: https://github.com/aarongreenlee/Learning-backbone.js. , : https://github.com/aarongreenlee/Learning-backbone.js/commit/ea4e61d934d2f987726720e81c479f9d9bb86e09#diff-2 ( ).
!