Javascript 将新模型添加到集合(backbone.js)时触发提取
我在外部发出POST请求并更新我的服务器,在我的移动应用程序中,我在发出POST请求的位置尝试发出GET请求 Backbone.js有listenTo事件,但它似乎只有在应用程序内发出POST请求时才有效,即在发出POST请求的同一位置 我可以在手动输入myCollection.fetch时更新我的应用程序,但我不确定如何让它自己触发。这似乎是一种必须由人工或setInterval函数触发的方法 下面是我在集合视图的initialize方法中所做的工作Javascript 将新模型添加到集合(backbone.js)时触发提取,javascript,backbone.js,Javascript,Backbone.js,我在外部发出POST请求并更新我的服务器,在我的移动应用程序中,我在发出POST请求的位置尝试发出GET请求 Backbone.js有listenTo事件,但它似乎只有在应用程序内发出POST请求时才有效,即在发出POST请求的同一位置 我可以在手动输入myCollection.fetch时更新我的应用程序,但我不确定如何让它自己触发。这似乎是一种必须由人工或setInterval函数触发的方法 下面是我在集合视图的initialize方法中所做的工作 //Attempt 1 this.list
//Attempt 1
this.listenTo(this.collection, 'add', this.collection.fetch);
initialize: function() {
var self = this;
//Attempt 2
this.listenTo(this.collection, 'add', this.collection.fetch({
success: function() {
self.render();
}
}));
this.render();
},
这些都不起作用,我甚至不确定它是否有效,但我的想法是倾听收藏的变化。我可能需要手动触发提取
编辑:这就是我目前正在尝试的,有没有比每3000毫秒发出一次GET请求更好的方法
编辑2:我添加了一个点击事件来获取数据,看起来手动获取是唯一的方法,类似于facebook/twitter刷新方法。您似乎无法侦听服务器上的更改
App.Views.PlayersView = Backbone.View.extend({
el: '.test',
events: {
'click .fetch': 'fetchMethod'
},
initialize: function() {
this.listenTo(this.collection, 'add', this.render);
this.render();
},
render: function() {
this.$el.empty();
this.collection.each(function(model) {
var playerView = new App.Views.PlayerView({ model: model });
this.$el.append(playerView.render().el);
}, this);
this.$el.append('<button class="fetch">Refresh</button>');
return this;
},
fetchMethod: function() {
var self = this;
this.collection.fetch({
success: function() {
self.render();
}
});
alert('new item so add was triggered');
}
});
是的,我越想它,我就发现我需要PHP或某种服务器解决方案。但是,这样做并不重要。最好是让用户手动刷新,再加上每隔10分钟的设置间隔。这对我来说非常好。我想大多数应用程序都是这样做的。你需要服务器向你的客户发送消息,告诉他们那里发生了什么。听起来你可能需要WebSocket或一些第三方解决方案,比如嘿,谢谢,pushover看起来很酷!
App.Views.PlayersView = Backbone.View.extend({
el: '.test',
events: {
'click .fetch': 'fetchMethod'
},
initialize: function() {
this.listenTo(this.collection, 'add', this.render);
this.render();
},
render: function() {
this.$el.empty();
this.collection.each(function(model) {
var playerView = new App.Views.PlayerView({ model: model });
this.$el.append(playerView.render().el);
}, this);
this.$el.append('<button class="fetch">Refresh</button>');
return this;
},
fetchMethod: function() {
var self = this;
this.collection.fetch({
success: function() {
self.render();
}
});
alert('new item so add was triggered');
}
});