2013-06-21 243 views
0

在我創建了以下型號,集合,並享有骨幹更新模型

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(){}

如何更新我的高速通道套接字函數我的骨幹模型?

回答

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 
});