我应该在哪里放置视图相关常量 -backbone.js

发布于 2024-11-09 15:32:52 字数 324 浏览 0 评论 0原文

想象一下,我有一个视图,作为视图的一部分,它渲染“x”模型对象,并且仅渲染“x”。问题是,我把这个与视图相关的常量放在哪里合适?

我的猜测是做这样的事情:

myApp.MyView = Backbone.View.extend({
    ...
    myConstant: 10,
    ...
    render: function(){
        ...
        //some code that uses myConstant
        ...
    }
});

这有意义吗?

任何建议都有帮助!

Imagine that I have a view, and as part of a view it render 'x' model objects, and 'x' only. The question, where is it appropriate for me to put this view related constant?

My guess would be to do something like this:

myApp.MyView = Backbone.View.extend({
    ...
    myConstant: 10,
    ...
    render: function(){
        ...
        //some code that uses myConstant
        ...
    }
});

Does this make sense?

Any suggestions help!

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

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

发布评论

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

评论(4

自由范儿 2024-11-16 15:32:52

听起来您想要做的是将类属性分配给视图。您可以将第二个哈希传递到扩展调用中来执行此操作。您的代码将如下所示:

myApp.MyView = Backbone.View.extend({

    render: function() {
        alert(myApp.MyView.myConstant);
    }

}, {

    myConstant: 10

});

您的常量可以作为 myApp.MyView.myConstant 访问。

It sounds like what you want to do is to assign a class property to the view. You can pass a second hash into your extend call to do this. Your code would look something like this:

myApp.MyView = Backbone.View.extend({

    render: function() {
        alert(myApp.MyView.myConstant);
    }

}, {

    myConstant: 10

});

where your constant is accessible as myApp.MyView.myConstant.

小情绪 2024-11-16 15:32:52

你非常接近!实际上,您只需使用 this.myConstant。这是一个工作示例...

testView = Backbone.View.extend({
    test: "hello world!",

    initialize: function(){
        alert( this.test );
        _.bindAll(this, "render");
    },

    render: function(){
        //do your rendering...
        return this;
    }
});
var view = new testView();

You're very close! You'll actually just use this.myConstant. Here is a working example...

testView = Backbone.View.extend({
    test: "hello world!",

    initialize: function(){
        alert( this.test );
        _.bindAll(this, "render");
    },

    render: function(){
        //do your rendering...
        return this;
    }
});
var view = new testView();
还在原地等你 2024-11-16 15:32:52

如上所述,我同意(并赞成)已接受的答案,但是为了清楚起见,我很想采用这种格式:

myApp.MyView = Backbone.View.extend({
    render: function() {
        alert(myApp.MyView);
    }
});

_.extend(myApp.MyView.prototype, {
  enum : {
      //...
  }
});

原因是您的视图中可能有很多 mixins,我发现当您继续添加时,可见性会变得模糊一个又一个的对象被View.extend。至少通过这种方式,您可以将其分解并在 mixins 之间添加注释。如果您使用 RequireJS 并加载一组通用的枚举/混合,那么它也更有意义。

As above I agree with (and upvoted) the accepted answer however for clarity I would be tempted to go with this format:

myApp.MyView = Backbone.View.extend({
    render: function() {
        alert(myApp.MyView);
    }
});

_.extend(myApp.MyView.prototype, {
  enum : {
      //...
  }
});

The reason being you might have lots of mixins for your view and I find the visibility becomes blurred when you just keep adding on object after object to View.extend. At least this way you can break it up and put comments in-between your mixins. It also makes a bit more sense if you're using RequireJS and loading in a common set of enums/mixins.

你丑哭了我 2024-11-16 15:32:52

不要在扩展下初始化视图常量,它对于所有视图实例的行为就像静态常量。请改用构造函数:

constructor: function()
{
     Backbone.View.prototype.constructor.apply(this, arguments);
     this.myConstant = 10;
     this.myOtherConstant = {};
}

更多说明这里

Don't initialize your view constants under extend, it will behave like static constant for all your view instances. Use constructor instead:

constructor: function()
{
     Backbone.View.prototype.constructor.apply(this, arguments);
     this.myConstant = 10;
     this.myOtherConstant = {};
}

more explanation here

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