BackboneJs - 模型或集合应该了解视图

发布于 2024-12-27 14:59:26 字数 892 浏览 5 评论 0原文

在我从此处的 SO 答案和许多 BackBoneJs 示例中挑选的示例之一中,我看到初始化函数知道模型将使用哪个视图进行渲染。我不知道我现在有点偏见,这是一个好的做法还是取决于正在开发的应用程序的类型。

示例

http://jsfiddle.net/thomas/Yqk5A/

编辑的小提琴

http://jsfiddle.net/Yqk5A/187/

代码参考

FriendList = Backbone.Collection.extend({
    initialize: function(){
        this.bind("add", function( model ){
            alert("hey");
            view.render( model );
        })
    }
});

上面是一个很好的做法还是下面

var friendslist = new FriendList;
var view = new FriendView({el: 'body'});
friendslist.bind("add", function( model ){
            alert("hey" + model.get("name"));
            view.render( model );
        })

在编辑的小提琴集合中由视图渲染,我们还可以使用更多视图来渲染集合。

In one of the example i picked from SO answers here and many BackBoneJs examples i see that the initialize function knows which view the model is going to be rendered with. I don't know i am kind of biased now, is this a good practice or it depends on type of application being developed.

Example

http://jsfiddle.net/thomas/Yqk5A/

Edited Fiddle

http://jsfiddle.net/Yqk5A/187/

Code Reference

FriendList = Backbone.Collection.extend({
    initialize: function(){
        this.bind("add", function( model ){
            alert("hey");
            view.render( model );
        })
    }
});

Is the above a good practice or below

var friendslist = new FriendList;
var view = new FriendView({el: 'body'});
friendslist.bind("add", function( model ){
            alert("hey" + model.get("name"));
            view.render( model );
        })

in the edited fiddle collection is rendered by a view, and we also can use many more views to render the collection.

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

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

发布评论

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

评论(2

合约呢 2025-01-03 14:59:26

我完全赞成使用事件,
我自己不想将绑定移到模型之外,我会像原始示例一样将它们保留在那里

var app = {};
app.evt = _.extend({}, Backbone.Events);  // adding a global event aggregator 

FriendList = Backbone.Collection.extend({
    initialize: function(){
        this.bind("add", function( model ){
            alert("hey");
            app.evt.trigger('addfriend', model);
        })
    }
});

//further in your code you can bind to that event
app.evt.bind("addfriend", function(model){
    var view = new FriendView({el: 'body'});
    view.render(model);
});

,但是,我发现这个示例有点奇怪,创建一个新视图,以主体作为元素,并通过给出模型到渲染函数。如果使用模型作为属性创建视图,然后将内容渲染到正文中,我会发现它更有逻辑。但这是另一个话题了。

简而言之,我在外部创建视图,监听正在触发的事件,但集合上的绑定保留在集合代码中。我发现将所有集合代码保存在同一个地方更易于管理。

I am all for using events,
I myself don't want to move the bind's outside the model, I'd keep them there like the original example

var app = {};
app.evt = _.extend({}, Backbone.Events);  // adding a global event aggregator 

FriendList = Backbone.Collection.extend({
    initialize: function(){
        this.bind("add", function( model ){
            alert("hey");
            app.evt.trigger('addfriend', model);
        })
    }
});

//further in your code you can bind to that event
app.evt.bind("addfriend", function(model){
    var view = new FriendView({el: 'body'});
    view.render(model);
});

however, i find the example a bit weird, creating a new view, with body as element, and rendering it with giving a model to the render function. i'd find it more logic if the view is created with a model as attribute, and then rendering the content into the body. but thats another subject all together.

in short, i move creating the view outside, listening to an event being triggered, but the bind on the collection stays in the collection code. i find it more managable to keep all the collection code at the same place.

維他命╮ 2025-01-03 14:59:26

我认为集合不应该知道用于渲染它的视图。我知道在我的项目中,同一个集合会以多种方式呈现,因此这种方法会迅速恶化。

一般来说,我将集合传递给呈现集合的视图,视图将侦听集合的添加/删除/更新事件以呈现元素。集合视图将了解子视图。

查看以下链接(系列中的第 3 篇博客),特别是 UpdatingCollectionView。这是我发现有用的方法。

http://liquidmedia.ca/blog/2011/02/backbone- js-第 3 部分/

I don't think the collection should know about the view that is used to render it. I know in my projects, the same collection is render in multiple ways so that approach would deteriorate rapidly.

In general I pass the collection to the view that renders the collection and the view will listen to add/remove/update events of the collection to render the elements. The collection view will have knowledge of the child view.

Check out the following link (3rd blog in a series) and in particular the UpdatingCollectionView. This is the approach that I've found useful.

http://liquidmedia.ca/blog/2011/02/backbone-js-part-3/

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