2013-01-07 62 views
1

我想進入node.js世界,並且想要構建一個簡單但完整的測試應用程序來使用redis將socket.io和backbone.js連接到node.js作爲商店。我找到了幾個教程和一些例子。不知何故,我只是對我必須使用的整個架構感到困惑。通常你會用express來定義你的server.js中的所有路由。所以你可以完全控制服務器端的路由。現在連接骨幹,你必須重新定義路線?這似乎是一個模範方面,但對我來說,這似乎是我不知何故不喜歡的雙重工作。 所以我只是感到困惑,事情完全不同?也許有人有一個很好的教程或例子,其中更清晰的鏈接。使用backbone.js與socket.io

回答

1

現在連接骨幹,你必須重新定義路由?

取決於你的路線。

您需要告訴骨幹網在哪裏可以找到服務器資源,因此模型可以告訴它在哪裏獲取它(模型中的url參數)。

骨幹中的路由類與服務器上的路由無關。這只是一種更改應用程序狀態或頁面內顯示的視圖的方法。

例如在一個LOB應用程序中,您有一個列表視圖和一個詳細視圖。

骨幹網允許您通過路由器在視圖之間切換而不刷新頁面

該列表的網址可能是http://app.com/#list,詳細視圖的網址可能是http://app.com/#detail/:id,其中id將是產品ID。您可以在視圖之間無需刷新頁面只要通過點擊定義爲

<a href="#detail/1">product 1</a> 

,並返回到列表視圖

<a href="#list">product list</a> 

那麼你可以有顯示產品形式的視圖鏈接切換

<a href="#newproduct">Create a new product</a> 

等等。所以您不必在視圖中設置事件偵聽器,以在不應該意識到彼此的視圖之間切換,並且不需要向服務器請求任何內容,也不需要刷新頁面。這是構建應用程序的一種方便的方式。

+0

感謝camus的回覆。它鬆了一下,然後在我腦海中結了一下。然而,我仍然失去了如何創建整體架構,以便流暢和看似無關的通信。我會試着讓自己更深入一點,然後希望弄清楚一些事情。 – Alx

1

我使用前端骨幹示範

class Model extends Backbone.Model 

    idAttribute: '_id' 

    _sync: (method, model, options) => 
     options.data ?= {} 
     @socket.emit method, @name(), model.toJSON(), options.data, (err, data) => 
      if err then console.error "error in sync with #{method} #{@.name()} with server (#{err})" else options.success(data) 

    sync: (method, model, options) => 
     if @socket.connected() is no 
      @socket.once 'connect', => @_sync method, model, options 
     else 
      @_sync method, model, options 

    name: => 
     if @collection and @collection.name then return @collection.name else throw new Error "Socket model has no name (#{@.collection})" 

    initialize: -> 
     @socket = require('socket') 

module.exports = Model 

類似的東西,這是我的基本骨幹收集

class Collection extends Backbone.Collection 

    model: require('class/socket/model') 

    _sync: (method, collection, options) => 
     @socket.emit method, @.name, collection.toJSON(), options.data, (err, data) => 
      if err then console.error "error in sync with #{method} #{@.name} with server (#{err})" else options.success(data) 

    sync: (method, collection, options) => 
     if @socket.connected() is no 
      @socket.once 'connect', => @_sync method, collection, options 
     else 
      @_sync method, collection, options 

    garbage: false 

    register: => 
     @socket.emit 'register', @name 

    deregister: => 
     @socket.emit 'deregister', @name 
     @garbage = true 

    initialize: (options) -> 
     @name = options.name if options and options.name 
     if [email protected] then throw new Error 'Socket collection has no name' 
     @socket = require('socket') 

     # Registrating socket for connection 
     @socket.on 'connect', => @register() if @garbage is off 
     if @socket.connected() is yes then @register() 

     @fetch()   

     # Registration for socket events for this collection 
     @socket.on @name, (method, model) => 

      if method is 'reset' 
       @reset(model) 

      if method is 'delete' 
       m = @get model._id 
       m.trigger 'destroy', m, m.collection 

      if method is 'update' 
       m = @get model._id 
       m.set(model) 

      if method is 'create' 
       @add(model) 

      console.log "SOCKET: " + method + " triggered for collection " + @name 

module.exports = Collection; 

這是CoffeeScript的,如果你不介意的話。

我看過的所有教程都使用register/deregister作爲集合。

重要的是找到一種方法來向這些集合和模型的後端引入驗證。這並不困難,但很棘手。

同樣要小心管理由同一個用戶的兩個套接字連接。它需要將套接字更新發送給同一用戶,但發送到另一個連接。