Hacker News new | past | comments | ask | show | jobs | submit login

For somebody new to MVC in JavaScript, how are models commonly implemented? Some kind of AJAX?



When building apps on Node.js, we use the same Backbone models and collections on both client and server. The difference is the Backbone.sync implementation:

* On client Backbone.sync does AJAX calls

* On server Backbone.sync talks to database

In some cases we also do Backbone.sync via socket.io

Note: Backbone.sync is the method all I/O operations on Backbone.js models and collections call. You can either use the default AJAX-based implementation that Backbone.js ships with, or override it with your own.


AJAX will be the lowest layer. Think of it as the database calls in an ORM like activerecord. You have an object, you do some validations or perform some logic and when you want to persist it on the server, you make an ajax call. In backbone you will call save on the model and it will do a POST or PUT request based on whether this is a new or already existing model.


Would this be a good MVC framework for Javascript: http://javascriptmvc.com/ ?


Yes. It is a popular one. You should also look at http://knockoutjs.com/ and http://maccman.github.com/spine/


Client-Side MVC isn't that widespread yet, so I'm not sure you could speak of a "common implementation" per se.

But models are generally just objects. They know how to handle and manage data. Nothing spectacularly complex.

As far as saving state goes there are different ways of doing it, the vanilla way backbone uses however seems to simply be making REST requests to the server.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: