在骨干网中,我创建了以下模型,集合和视图
var sensor= Backbone.Model.extend({})
var sensorCollection = Backbone.Collection.extend({
model: sensor
})
var sensors= new sensorCollection;
var SensorView =Backbone.View.extend({})
var AppView =Backbone.View.extend({})
var App = new AppView
我还运行了这个PHP套接字:
var conn = new ab.Session('ws:localhost:8080', function() {
conn.subscribe('temperature', function(topic, data) {
console.log('Current temperature reading' + data.temperature);
sensor.set({temperature:data.temperature});//Attempting to change backbone model
});
}, function() {
console.warn('WebSocket connection closed');
}, {
'skipSubprotocolCheck' : true
});
我正在尝试使用插槽中的温度读数来更改/更新模型。如您所见,我已经尝试了sensor.set
并尝试了以下内容:
App.model.set
This.model.set
App.sensor.set
每种不同的方式都表示未定义或未找到的功能。我在这里错过了什么吗?它们都在同一$(function(){}
如何从我的Autobahn套接字功能更新我的骨干模型?
答案 0 :(得分:0)
Backbone.Model.extend({})
返回一个类,而不是实例。你应该写:
var Sensor = Backbone.Model.extend({}); var sensor = new Sensor(); sensor.on('change:temperature', function (temperature) { console.log('new temperature: ' + temperature); }); var conn = new ab.Session('ws:localhost:8080', function() { conn.subscribe('temperature', function(topic, data) { console.log('Current temperature reading' + data.temperature); sensor.set({temperature:data.temperature});//Attempting to change backbone model }); }, function() { console.warn('WebSocket connection closed'); }, { 'skipSubprotocolCheck' : true });