javascript - Re-using Backbone.js models on the server side with Node.js and Websockets - Stack Overflow

I've been working my way through:I have a few questions about sharing models server-side and a few

I've been working my way through:

I have a few questions about sharing models server-side and a few questions about overriding sync. Real-time model syncing architecture ftw.

  1. Models
    So in this example he syncs his whole application state. Part of my application state is the User model, this handles things like logging in, finding the type of platform they are using, etc. Am I using this wrong? I have client side session data in this model, something that really doesn't need to be on the server, should I put this on the server anyway?

    For other models that are strictly application data that should be synced with the server, how do I manage these model on the server? Is there a "view" type ponent that handels changes to the model and acts on the model how the server needs to?

  2. Sync
    I want to override the sync method to sync with the server and any other clients that need the updated data. How could you write a sync method that works client -> server and server -> client so that no matter where it is called everyone get updated?

I've been working my way through:

http://blog.andyet./2011/02/15/re-using-backbonejs-models-on-the-server-with-node

I have a few questions about sharing models server-side and a few questions about overriding sync. Real-time model syncing architecture ftw.

  1. Models
    So in this example he syncs his whole application state. Part of my application state is the User model, this handles things like logging in, finding the type of platform they are using, etc. Am I using this wrong? I have client side session data in this model, something that really doesn't need to be on the server, should I put this on the server anyway?

    For other models that are strictly application data that should be synced with the server, how do I manage these model on the server? Is there a "view" type ponent that handels changes to the model and acts on the model how the server needs to?

  2. Sync
    I want to override the sync method to sync with the server and any other clients that need the updated data. How could you write a sync method that works client -> server and server -> client so that no matter where it is called everyone get updated?

Share Improve this question edited Jan 6, 2015 at 6:59 Sangharsh 3,0292 gold badges16 silver badges28 bronze badges asked Sep 18, 2011 at 23:22 fancyfancy 51.5k64 gold badges158 silver badges230 bronze badges 10
  • 4 I would remend against using backbone on the server. It's a nice idea but it just does not work. It's an abstraction that leaks and get's in the way – Raynos Commented Sep 18, 2011 at 23:45
  • @Raynos Can you give me some more info why you say this, and how you would go about implementing it? – fancy Commented Sep 19, 2011 at 0:24
  • 1 personal experience trying to use backbone says it get's in the way. Real projects say that it's not a good tool to use. It's developed for the client, there are better patterns for the server. – Raynos Commented Sep 19, 2011 at 0:27
  • 1 @Raynos thanks, any chance you could point me toward some resources explaining a pattern that would work well service-side in this situation? Should I go about it a similar way (mvc) but just build it from scratch? – fancy Commented Sep 19, 2011 at 0:30
  • 2 @Raynos I could see how backbone would feel redundant if your webapp was talking directly to the datastore. Where backbone on the server has really worked for us is when the webapp is talking to one or more REST APIs as datastores. So, if you have an app that's talking to a REST api, and there's not already client libraries for it, Backbone works really well as a quick and useful form of generic client library. – Edward M Smith Commented Sep 19, 2011 at 21:42
 |  Show 5 more ments

1 Answer 1

Reset to default 4

There are a a couple of aspects that make backbone a very good fit for client applications, but not not at all useful for server-based environments.

The core of backbone is its Events module, which the framework is built around (models are basically event-managed collections, views are glue code for rendering based on model event changes, etc...), is pretty much useless on a server: the only real event you're ever getting is the request, or the various events for socket data, and both are handled by (and are taken care of for) by the middleware and node itself.

Models:

  • if you're using some kind of ORM on your server, it should already provide the event handling necessary for dealing with model changes. And since you dont do any dynamic view updates on the server, you don't need any of the infrastructure backbone provides for models.

  • if you aren't using an ORM (as in realtime, never logged chat :), you can use Backbone's models, but they aren't suited for larger datasets or any kind of storage, and in the end you're still using an underscore-wrapped Hash / Array.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744131135a4559847.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信