Mayhem93
Mayhem93

Reputation: 132

Build backbone models from existing html

I'm not particularly good at javascript frameworks, I decided to use Backbone to help organize my code since my project got bigger.

The problem is, I didn't had javascript templates (or Views) in mind when I designed the first 10k lines on my project. I'm already using a templating engine (PHP smarty).

The premises: I already have a good HTML template. As an example I have an <article> element which contains the content of a blog post. It's ok, because I can build a Model out of this element. But what about the View? I don't have a template nor do I want to actually use one (because I have to reorganize everything).

The question is: how do I bind all the possible variables in an <article> (which could be for example the value of <timestamp> element, outerHTML of the content div, etc) to an object view?

All the examples, tutorial I've seen, uses a template which is used to generate the HTML. I'm not really comfortable in defining a underscore.js template.

The CRUD operations on articles, comments, etc would be done with $.ajax(). I don't want to do additional http requests just to get all the posts of a page (since the backend already does this).

Upvotes: 1

Views: 225

Answers (1)

lecstor
lecstor

Reputation: 5707

You can use the setElement method to do what you want (or at least attach your view to an existing element in the dom, then your view will need to know what to do with it)

http://backbonejs.org/#View-setElement

Upvotes: 1

Related Questions