1

I have a project where I load parts of my content over an ajax request. I get everytime the same structures of information (json) and I tried to make a coffeescript class for each DOM-Layout which presents these structures.

Im working on the first DOM-representation for these structures and my code is blowing up by statements like controls.append @Legende.content.messageSpan or @Legende.content.itself.append $(labelNew).html "Name: "

The problem: There are 3 following DOM-representations i have to implement and that will blow up my code.

My question is as follows: What is the main practice to handle many DOM-tree changes and/or prevent enlarging the number of files and increased file sizes?

4

1 回答 1

0

如果你真的有大量基于底层模型更改的 DOM 更改,最好使用一个库,它允许你将 dom 隐式绑定到模型,而无需显式更改。2个最受欢迎的图书馆是

  • knockoutjs仅此而已
  • angularjs做这个以及你可能需要或不需要的很多其他东西

KnockoutJS 通常更简单、更平易近人。

Angular 有点复杂和固执己见,但随着项目变得越来越大,它的规模也越来越大。

这些库通过像这样向 html 添加注释来工作(基于 Knockout 的示例)

<tr>
    <td><input data-bind="value: name" /></td>
    <td><select data-bind="options: $root.availableMeals, value: meal"></select></td>
    <td data-bind="text: formattedPrice"></td>
    <td><a href="#" data-bind="click: $root.removeSeat">Remove</a></td>
</tr>   

您可以根据自己的喜好设置 dom,它会自动更改以反映您绑定的数据。

于 2013-06-13T12:11:19.793 回答