我是否有理由在 Backbone.js 中避免这种情况?

发布于 2024-10-06 22:20:50 字数 2101 浏览 2 评论 0原文

我越来越喜欢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);
        }
   }
}

我的问题

  1. 当我学习backbone.js时,我是否遗漏了一些东西,这将使我能够自然地做到这一点?
  2. 我有理由避免这种情况吗?

其他信息

我已在 GitHub 上分享了该应用程序(相当初级):https: //github.com/aarongreenlee/Learning-backbone.js。如果您希望在该环境中查看代码,可以在此处访问:https://github.com/aarongreenlee/Learning-backbone.js/commit/ea4e61d934d2f987726720e81c479f9d9bb86e09#diff-2(初始提交)。

感谢您的时间和帮助!

I am enjoying backbone.js more and more. I desire to have multiple views for a given model:

  • A list-view where each model has a view with it's own li element.
  • A detail view showing all of the details of a model.

My question is, I was seeking a good way to allow one view to communicate with another and elected the following:

/** 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);
        }
   }
}

My Questions

  1. Am I missing something as I learn backbone.js which will allow me to do this nativly?
  2. Is there a reason I should avoid this?

Additional Info

I have shared the application (quite rudimentary) on GitHub: https://github.com/aarongreenlee/Learning-backbone.js. If you prefer to see the code in that environment, you can access it here: https://github.com/aarongreenlee/Learning-backbone.js/commit/ea4e61d934d2f987726720e81c479f9d9bb86e09#diff-2 (initial commit).

Thank you for your time and help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟酉 2024-10-13 22:20:50

那么你的视图可以有一个像树一样的参考,但你的模型不应该知道你的视图!

您应该将视图设置为侦听模型中的更改事件并让它们做出相应的反应(即重新渲染)。

这样,您就可以避免软件的较低部分(应该坚如磐石的模型)和较高部分(视图)之间的交叉引用。经典的MVC分离。

因此,将 addViews、removeViews 移至 Backbone.View 应该没问题。您将创建一个分层视图系统,就像 sproutcore 提供的那样。

玩得开心!

Well your views can have a tree like reference but your model shouldn't know about your views!

You should set your views to listen to change events from the models and have them react accordingly (re render that is).

This way you avoid cross reference between lower parts of your software (the one that should be rock solid, the model) and the higher parts, the views. Classic MVC separation.

So move your addViews, removeViews to Backbone.View and it should be good. You will be creating a hierarchical view system, much like what sproutcore offers.

Have fun!

ゝ杯具 2024-10-13 22:20:50

Document Cloud 的 Jeremy Ashkenas 从 Twitter 给出了答案

@aarongreenlee 没有理由避免它
-- 如果您想让您的视图彼此保持树状引用,
这当然是合法的。 -@jashkenas

An answer came in from Twitter from Jeremy Ashkenas of Document Cloud

@aarongreenlee No reason to avoid it
-- if you want to have your views keep a tree-like reference to one another,
that's certainly legit. -@jashkenas

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文