gpt4 book ai didi

javascript - 在backbone.js 中创建实时集合?

转载 作者:行者123 更新时间:2023-11-28 01:29:38 26 4
gpt4 key购买 nike

我正在创建一个应该支持多个用户的基本主干应用程序,并且我希望当任何人对集合进行更改时,我的 Collection View 能够为所有用户更新。

我正在执行以下操作:

在我看来:

/*Collection view
- - - - - - -*/
App.Views.Contacts = Backbone.View.extend({
tagName: 'tbody',
wrapper : $('#contacttable'),
initialize: function() {

// custom events
vent.on('hideparent', this.hideMe,this);
vent.on('showparent', this.showMe,this);

// standard events
this.listenTo(this.collection, 'add', this.addOne);
this.listenTo(this.collection, 'reset', this.addAll);
this.listenTo(this.collection, 'all', this.render);

// Load the collection data
this.collection.fetch({reset:true});
},
render: function() {
// publish view status
vent.trigger();
// append the list view to the DOM
$('#contacttable').append(this.el);
return this;
},
addAll : function() {
this.collection.each( this.addOne, this);
},
addOne: function(model) {
var contactView = new App.Views.Contact({ model: model});
this.$el.append(contactView.render().el);
},
showMe: function() {
this.wrapper.show();
},
hideMe: function() {
this.wrapper.hide();
}
});

/*Single view
- - - - - - - */
App.Views.Contact = Backbone.View.extend({
tagName: 'tr',
template: template('singlecontact'),
events: {
'click a.delete' : 'deleteContact'
},
initialize: function() {
this.model.on('destroy', this.unrender, this); // doing this so item is only removed visually if its actually destroyed
this.model.on('change', this.render, this);
},
render: function() {
this.$el.html( this.template( this.model.toJSON() ) );
return this;
},
deleteContact: function(e) {
e.preventDefault();
this.model.destroy();
},
unrender: function() {
this.remove();
}
});

在我的路由器中:

这适用于添加和编辑但不适用于删除,这是一个问题1。它也感觉不是特别正确。

contactList: function() {
vent.trigger('app:contacts');
if (typeof App.AllContactsView == 'undefined') {
App.AllContactsView = new App.Views.Contacts({ //init method runs now
collection : App.contacts
});

// checking for updates
function updateContacts() {
App.AllContactsView.collection.fetch({merge:true });
}

setInterval(updateContacts, 1000);

} else {
App.AllContactsView.collection.fetch({add:true,remove:true});
}

ViewManagerTwo.showView(App.AllContactsView,true,false, false);
return App.AllContactsView;
},

是否有一种本地方式 - 理想情况下比这更好地实时更新集合?

最佳答案

以下是有关在 Backbone 中使用 websockets 作为传输层的一些信息,应该可以帮助您入门:

http://lostechies.com/derickbailey/2012/04/19/decoupling-backbone-apps-from-websockets/

How to use backbone.js with websockets/socket-io/nowjs

关于javascript - 在backbone.js 中创建实时集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22287505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com