2013-01-07 5 views
1

나는 node.js 세계에 들어가려고 노력하고 있으며, redis를 사용하여 node.js와 socket.io 및 backbone.js를 연결하기위한 간단하면서도 완벽한 테스트 앱을 만들고 싶습니다. 가게로. 나는 몇 가지 자습서와 몇 가지 예를 발견했다. 어쨌든 나는 사용해야하는 전체 아키텍처에 대해 혼란스러워합니다. 일반적으로 express로 server.js 내의 모든 경로를 정의합니다. 따라서 서버 측에서 경로를 완전히 제어 할 수 있습니다. 백본을 연결하면 경로를 다시 정의해야합니까? 이것은 모델 측면 인 것 같지만, 제게 그것은 제가 어떻게 든 싫어하는 이중 작업처럼 보입니다. 그래서 혼란스러워졌고 완전히 다른 방식으로 작동합니까? 어쩌면 누군가 더 좋은 지침서 나 예제에 대한 링크를 가지고있을 것입니다.backbone.js를 socket.io와 함께 사용하기

답변

1

백본을 연결하면 경로를 다시 정의해야합니까?

은 경로의 의미에 따라 다릅니다.

서버 리소스를 찾을 위치를 백본에게 알려줘야 모델이 모델 리소스를 가져올 위치 (모델의 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> 

당신이 제품의 양식을 표시하는 뷰를 가질 수에 정의 된 링크를 클릭하여 페이지를 새로 고치지 않고보기 사이를 전환 할 수 있습니다

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

등등. 따라서 뷰에 이벤트 리스너를 설정하여 서로에 대해 인식하지 않아야하는 뷰와 서버에 아무 것도 요청하지 않고 페이지를 새로 고치지 않고 뷰간에 전환 할 필요가 없습니다. 응용 프로그램을 구성하는 데 편리한 방법입니다. 내가 프런트 엔드 백본 모델

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; 

당신이 괜찮다면 그것은 커피 스크립트있어입니다

+0

답장을 보내 주셔서 감사합니다. 그것은 내 머리에서 약간 매듭을 풀어 준다. 그러나 저는 유창하고 겉으로 드러난 공감대를 갖기 위해 전반적인 아키텍처를 만드는 방법을 여전히 잃어 버렸습니다. 나는 백본 자체에 좀 더 깊숙이 들어가려고 노력할 것이고, 그런 다음 희망을 찾을 것이다. – Alx

1

.

내가 본 모든 자습서는 컬렉션에 등록/등록 취소를 사용합니다.

중요한 것은 이러한 컬렉션과 모델을 사용하여 백엔드에 인증을 도입하는 방법을 찾는 것입니다. 그리 어렵지는 않지만 까다 롭습니다.

또한 동일한 사용자가 두 개의 소켓 연결을 관리하는 데주의해야합니다. 동일한 사용자에게 소켓 업데이트를 보내야하지만 다른 연결에는 소켓 업데이트를 보내야합니다.

관련 문제