通过socket.io使用backbone.js

我试图进入node.js世界,并想build立一个简单但完整的testing应用程序来连接node.js与socket.io和backbone.js使用redis作为商店。 我发现了一些教程和一些例子。 不知何故,我只是对我必须使用的整个架构感到困惑。 通常你会用express来定义你的server.js中的所有路由。 所以你可以完全控制服务器端的路由。 现在连接骨干,你必须重新定义路线? 这似乎是一个模范的方面,但对我来说,这似乎是我不知何故不喜欢的双重工作。 所以我只是感到困惑,事情完全不同? 也许有人链接到一个很好的教程或例子,它更清晰。

现在连接骨干,你必须重新定义路线?

取决于你的路线是什么意思。

你需要告诉骨干在哪里find服务器资源,所以对于模型来说,告诉它在哪里得到它(模型中的URL参数)是有意义的。

骨干网中的路由类与服务器上的路由无关。 这只是一种更改应用程序状态或页面内显示的视图的方法。

例如在一个LOB应用程序中,您有一个列表视图和一个详细视图。

主干允许您通过路由器在视图之间切换, 而无需刷新页面

该列表的url可能是http://app.com/#list ,详细信息视图的url可能是http://app.com/#detail/:id ,其中id将是产品ID。 您可以在不刷新页面的情况下切换视图,只需点击定义为的链接即可

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

并回到列表视图

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

那么你可以有一个显示产品forms的视图

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

等等。 因此您不必在视图中设置事件侦听器,以在不应该意识到彼此的视图之间进行切换,并且不需要向服务器请求任何内容,也不需要刷新页面。 这是一个方便的方式来构build你的应用程序。

我正在使用类似的前端骨干模型

 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 !@name 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作为集合。

重要的是find一种方法来引入身份validation到后端与这些集合和模型。 这不是那么难,但是很棘手。

还要小心pipe理同一用户的两个套接字连接。 它需要将套接字更新发送给同一个用户,而不是另一个连接。