我越來越喜歡backbone.js。我希望對於給定模型有多個視圖:有沒有理由在Backbone.js中避免這種情況?
- 列表視圖,其中每個模型都有一個包含它自己的li元素的視圖。
- 一個詳細視圖,顯示模型的所有細節。
我的問題是,我正在尋求一個很好的方式,讓一個視圖與另一個溝通,選出了以下:
/** Allow a model to keep track of it's views. **/
Backbone.Model.prototype.addView = function (view) {
// Ensure our model has a view array.
if (typeof this.views === 'undefined')
{
this.views = [];
}
// Append our newest view to the array only if it is not already present.
if (_.indexOf(this.views, view) === -1)
{
this.views.push(view);
}
}
/** Allow a model to remove all of it's views.
*
* @param {Object} args Any arguments will be provided to the view's method.
*/
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);
}
}
}
/** Allow a model to re-render all of it's views.
*
* @param {Object} args Any argyments will be provided to the view's method.
*/
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);
}
}
}
我的問題
- 我缺少的東西當我學習backbone.js,這將讓我做到這一點本土?
- 我有什麼理由避免這種情況?
附加信息
我已經在GitHub上共享的應用程序(相當不成熟):https://github.com/aarongreenlee/Learning-backbone.js。如果您希望在該環境中查看代碼,可以在此訪問它:https://github.com/aarongreenlee/Learning-backbone.js/commit/ea4e61d934d2f987726720e81c479f9d9bb86e09#diff-2(初始提交)。
謝謝你的時間和幫助!
+1將我轉到Backbone.js。 – Stephen 2010-12-10 19:13:59