Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it make sense to initialize models from the DOM in backbone.js?

backbone.js relies on restful applications to initialize models, but what about progressive enhancement? The data is already in the dom (or some of it), so should my models still make calls the the restful interface even though the html elements exist? Is there another library design that might be better suited for this case?

like image 954
Derek Reynolds Avatar asked Feb 23 '11 23:02

Derek Reynolds


1 Answers

Backbone can handle that pretty well. The way I handle this case is to have a factory model that can receive a DOM node and parse it in order to extract data (id, fields and so on).

If you supply a 'el' option to a View constructor, backbone won't fetch nor render the model, so you can keep your node as is.

Upon data change, the controller will then sync to the server. You must be careful though to include whatever data your application needs to function whether it's displayed or not.

like image 199
Arthur Debert Avatar answered Oct 06 '22 00:10

Arthur Debert