2012-12-08 70 views
0

帶Mongoose/Express的Backbone.js。Backbone.js Node.js在服務器上訪問模型

我有最難的時間瞭解如何與服務器上的數據庫和模型進行通信。老實說,我不明白模型和服務器的關係。客戶機上的模型是否與服務器上的模型同步?服務器上是否有模型?目前MongoDB中存在填充數據,我試圖做的就是讓fetch()工作。任何幫助都會很棒。當Backbone已經完成所有這些工作時,我試圖避免使用RESTful調用。

// CLIENT 
// Lobby.js 

(function($){ 

    var socket = io.connect('http://localhost:3000'); 

    var App = Backbone.Router.extend({ 
     routes: { 
      '': 'lobby' 
     }, 
     lobby: function() { 
      var collection = new Collection(); 
      var listView = new MatchListView(collection); 

      collection.fetch(); 

      collection.fetch({success: function(){ 
       console.log("Fetch Success"); // => 2 (collection have been populated) 
      }}); 
     } 
    }); 

    var Model = Backbone.Model.extend(); 

    /** 
    * Collection - bound to the server 
    * matchListView is listening for event changes 
    */ 
    var Collection = Backbone.Collection.extend({ 
     url: 'lobby', 
     socket:socket, 
     model: Model, 
     initialize: function(Collection){ 
      _.bindAll(this, 'addOne', 'removeOne', 'removeOne'); 

      this.ioBind('createMatch', this.addOne, this); 
      this.ioBind('removeMatch', this.removeOne, this); 
     }, 
     addOne: function(data) { 
      this.add({id:data._id}); 
     }, 
     removeOne: function(data) { 
      console.log('remove match ' + data._id); 
      this.remove({id:data._id}); 
     } 
    }); 

    /** 
    * View - bount to collection 
    * listening for changes to collection 'add' and 'remove' 
    */ 
    var MatchListView = Backbone.View.extend({ 
     el: $('body'), 
     urlRoot: 'lobby', 
     socket:socket, 
     events: { 
      'click #create': 'createMatch' 
     },  
     initialize: function(Collection){ 
      _.bindAll(this, 'render', 'renderCollection','addOne', 'removeOne', 'createMatch'); 

      this.collectionView = Collection; 
      this.collectionView.bind('add', this.addOne); 
      this.collectionView.bind('remove', this.removeOne); 

      this._viewPointers = {}; // make sure we're starting over 
      this.render(); 
     }, 
     render: function(){ 
      ... 
     } 
    }); 


    $(document).ready(function() 
    { 
     // create a new app and trigger the router. 
     window.app = new App(); 
     Backbone.history.start(); 
    }); 

})(jQuery); 

上面的頁面住在/大堂。服務器上的mongoDB和它的模式生命在/ mongo

//SERVER 
// Mongo.js 

/** 
* Mongol Database 
*/ 
var mongoose = require('mongoose'); 
var db = mongoose.createConnection('mongodb://localhost:27017/test'); 
//var db = mongoose.createConnection('mongodb://nodejitsu_cpiv:[email protected]:43927/nodejitsu_cpiv_nodejitsudb7525674102'); 
db.on('error', console.error.bind(console, 'connection error:')); 
db.once('open', function callback() 
{ 
    console.log("connected to database tester"); 
}); 

/* ==================== 
// Lobby 
// =================== */ 
var schema = mongoose.Schema, 
    ObjectId = schema.ObjectId; 

var lobbySchema = new schema({ 
    status:Number, 
    sockets: [{ id:String, team:Number}], 
    player1:{id:Number}, 
    player2:{id:Number} 
}); 


// Collection 
var Lobby = db.model('lobby', lobbySchema); 

回答

3

這裏有一種方法來思考它。

模型代表了更多或更少一致的架構(statussockets,等你的情況)的數據記錄。這些記錄在MongoDB中正式生存。 Mongoose爲您提供了創建它們,查詢它們,更新它們以及從node.js中的服務器代碼中刪除它們的便捷方法。所以在服務器端,數據依賴於MongoDB,您定義的Mongoose Schema和Models是與數據交互和操作的方式。

在瀏覽器中,代表數據記錄的模型的概念相同,但您使用Backbone創建它們,查詢它們,更新它們並刪除它們。從瀏覽器的角度來看,代替MongoDB,規範數據記錄存在於另一個REST API調用中,這個骨幹網將幫助您完成。

現在,服務器上的Mongoose模型到客戶端上的骨幹模型的映射不是自動的。這只是一個推薦的架構。您必須適當地將代碼與URL和屬性連接起來,以便根據需要或多或少地進行匹配。有些差異是可以的,例如在服務器上的用戶模型上有bcryptedPassword字段,但從不將該屬性發送到瀏覽器,因爲這樣做不必要也不安全。

您的代碼摘錄中的幾個快速點。

您無緣無故地致電fetch()兩次。您還需要this.collectionView.bind('add', this.addOne);this.collectionView.on('add', this.addOne);bind用於修復方法的上下文thison/off用於註冊事件處理程序。

相關問題