Backbone.js - 从现有 html 实例化模型/视图
我今天开始研究backbone.js,作为更好地组织应用程序中代码的一种方法。
我想知道(概念上 - 所以无论如何用伪代码回复)我将如何使用我的现有 html 来创建主干模型(和视图)。
我发现的所有教程都包括使用空白 html 模板,然后使用 ajax 注入内容。我不想这样做。
假如我有一本藏书。
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>My Book Collection</title>
</head>
<body>
<head>
</head>
<body>
<ul id="bookCollection">
<li class="book" data-book-id="1"><input type="text" name="book_1_name" value="My Book A"/></li>
<li class="book" data-book-id="2"><input type="text" name="book_2_name" value="My Book B"/></li>
<li class="book" data-book-id="3"><input type="text" name="book_3_name" value="My Book C"/></li>
<li class="book" data-book-id="4"><input type="text" name="book_4_name" value="My Book D"/></li>
<li class="book" data-book-id="5"><input type="text" name="book_5_name" value="My Book E"/></li>
</ul>
</body>
</body>
</html>
在这个阶段,我想开始将每本书作为一个模型进行管理,每当书名发生更改时调用一个函数(只是函数中的一个警报以进行概念验证),然后调用一个 URL 将模型的更改与我的数据库同步。
任何人都可以为我指出正确的方向,以找到使用页面上现有的 html 执行上述操作的方法吗?
如果它有所作为,我计划使用小胡子作为我的模板。
I've started looking at backbone.js today as a way to better organize the code in my application.
I was wondering (conceptually - so reply with pseudocode by all means) how I would use my existing html to create Backbone Models (and Views).
All of the tutorials I've found consist of using a blank html template and then injecting in the content using ajax. I don't want to do this.
If I have a collection of books.
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>My Book Collection</title>
</head>
<body>
<head>
</head>
<body>
<ul id="bookCollection">
<li class="book" data-book-id="1"><input type="text" name="book_1_name" value="My Book A"/></li>
<li class="book" data-book-id="2"><input type="text" name="book_2_name" value="My Book B"/></li>
<li class="book" data-book-id="3"><input type="text" name="book_3_name" value="My Book C"/></li>
<li class="book" data-book-id="4"><input type="text" name="book_4_name" value="My Book D"/></li>
<li class="book" data-book-id="5"><input type="text" name="book_5_name" value="My Book E"/></li>
</ul>
</body>
</body>
</html>
At this stage I would like to start managing each book as a model, calling a function whenever book names are changed (just an alert in the function for proof of concept), and then calling a URL to sync changes to the model with my database.
Can anyone point me in the right direction for a way to do the above using existing html on a page?
If it makes a difference I plan on using mustache for my templating.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我真的也想做同样的事情,只是找到了解决办法!
试图构建一个待办事项列表示例,其中页面上已经有一些待办事项,希望将它们作为模型放入我的待办事项集合中,并以与添加到空白页面的元素相同的方式管理它们。
整个 js 代码作为要点粘贴在 https://gist.github.com/1255736 中,并带有注释以解释更多信息。
重要的部分是如何实例化集合。基本上:
注意:集合通常会稍后绑定到视图,以便使用 collection.add 也会更新视图。由于在构造函数中调用了初始化,因此集合尚未绑定,并且您不会通过在此处添加元素来重复 HTML 中的元素。
希望这有帮助!
I was really trying to do the same thing as well and just found my way around it!
Was trying to build on a todo list example where I'd already have some todos on the page, want to bring them as models in my Todos collection and have them managed the same way it happens for elements that were added to a blank page.
The whole js code is pasted as a gist there https://gist.github.com/1255736 with comments to explain more.
The important part is on how to instantiate the collection. Basically:
Note: the collection is usually tied to a view later so that using collection.add will update the view as well. Since initialize is called in the constructor, the collection has not been bound yet and you won't duplicate elements in your HTML by adding them here.
Hope this helps!
Backbone 的视图始终绑定到特定的 html 元素(视图的属性
el
)。您可以将BookCollectionView
绑定到ul#bookCollection
并将BookView
绑定到li.book
,这应该适合您当前的模板结构。您可以使用模型的 url 将
Book
模型映射到视图。如果从该 url 获取模型,并且您已为模型更改定义了事件绑定,则相应的视图应使用新模型数据刷新。这同样适用于馆藏的网址和书籍收藏。我猜关于主干的好的教程并不多,但是可以学习一些类似的东西 http://liquidmedia.ca/blog/2011/02/backbone-js-part-3/ 或 http://www.jamesyu.org/ 2011/02/09/backbone.js-tutorial-with-rails-part-2/。如果你能提出一些更具体的问题,我猜会更容易!
Backbone's views are always bound to a specfic html element (the view's attribute
el
). You could have something like aBookCollectionView
bound toul#bookCollection
and aBookView
bound toli.book
, which should be fine with your current template structure.You can map a
Book
model to a view using a url to the model. If the model is fetched from that url and you have defined an event binding for the model change, the according view should refresh with the new model data. Same applies for the collection's url and a collection of books.There are not many good tutorials on backbone out i guess, but study something like http://liquidmedia.ca/blog/2011/02/backbone-js-part-3/ or http://www.jamesyu.org/2011/02/09/backbone.js-tutorial-with-rails-part-2/. Guess it is easier if you can come up with some more concrete questions!
我遇到了同样的问题,并在我的主视图(ul 列表)的构造函数中以这种方式解决了它 - 从骨干 0.9 开始。
我在脑海中从coffeescript语法转换了它,所以如果有一些语法错误,请保持温和。
并像这样调用它:
重要的是,集合“anExistingCollection”的顺序与您已经存在的顺序相同生成的列表条目,因为本示例依赖于相同的索引。
(未经测试)
I had the same problem and solved it this way within the constructor of my main view (the ul list) - as of backbone 0.9.
I converted it in my mind from coffeescript syntax so please be gentle if there are some syntax errors.
and calling it like this:
It's important that the order of the collection 'anExistingCollection' is the same as your already generated list entries as this example relies on the same index.
(untested)