从 Mustache 模板访问嵌套主干模型属性 [英] Access Nested Backbone Model Attributes from Mustache Template
问题描述
我有一个 Backbone 模型,它的一个属性是对另一个 Backbone 模型的引用.例如,一个 Person 有一个对 Address 对象的引用.
I have one Backbone model which has an attribute that is a reference to another Backbone model. For example, a Person has a reference to an Address object.
Person
FirstName
LastName
Address
Street
City
State
Zip
这些是扩展 Backbone 模型的类.那么,如果我构造一个如下所示的对象......
These are classes that extend the Backbone model. So, then if I construct an object like the following...
var address = new Address({ Street: "123 Main", City: "Austin" });
var person = new Person({ FirstName: "John", Address: address });
我似乎不知道如何在我的 Mustache 模板中访问它.
I cannot seem to figure out how to access it in my Mustache template.
Hi {{FirstName}}, you live in {{Address.City}}.
显然不起作用.当我查看 Firebug 的内部结构时,Address 是一个对象,而 City 是 Address 的属性对象中的一个属性.我找不到有关如何访问关联对象的这些属性的任何示例.
Obviously does not work. When I look at the internals in Firebug, Address is an object, but the City is an attribute within the attributes object of Address. I cannot find any examples of how to access these attributes of associated objects.
感谢您的帮助!谢谢!
推荐答案
尝试使用 Handlebars,一个基于具有嵌套属性支持的 Mustache.
Try using Handlebars, a templating engine based on Mustache with nested properties support.
然后就像{{Address/City}}
一样简单.
如果您不想更改模板引擎,可以将 Address
对象的结果展平,并将它们作为属性直接传递给 Person
.
If you don't want to change your templating engine, you can flatten results from Address
object and pass them as properties directly on the Person
.
这篇关于从 Mustache 模板访问嵌套主干模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!