使用 backbone.js:
我有一个顶级的 ModelA,它包含2个属性和2个嵌套模型,ModelB 和 ModelC。ModelB 和 ModelC 各有以下两个属性:
ModelA
attributeA1
attributeA2
ModelB
attributeB1
attributeB2
ModelC
attributeC1
attributeC2
有一个用于 ModelA 的 ViewA 和一个用于 ModelB 的 ViewB。 ViewA 的呈现函数在主体上放置一个新的 div,而 ViewB 的呈现函数创建一个 h1。ViewA 的初始化调用 ViewB 的呈现将 h1插入到新的 div 中。这种分离的基本原理是 h1可能会发生变化,并且需要独立于 ViewA 的重新呈现。
ViewA
initialise:
//call ViewA's own render function
this.render()
//call ViewB's render function that further modifies the $("#new") div created earlier.
$("#new").append(ViewB.render().el)
//ViewA's own render function
render: //place <div id="new"></div> onto 'body'
ViewB
render: //create a <h1></h1>
funcB1: //can this access it's parent ModelA's attributes and other objects?
问题1: ViewB 有一个 function B1函数。这个函数可以访问它的父模型的属性吗?属性,例如 AttributeA1,或者甚至 AttributeC1(它是兄弟姐妹/堂兄弟姐妹) ?
Q2: 作为 Q1的进一步扩展,function B1能访问与 ViewA 相关的 DOM 元素吗?(在本例中,是 # new div?)
Q3: 一般来说,我如何定义上面描述的视图和模型之间的关联,以便所有东西都能正确地联系在一起?
我意识到这个问题有点抽象,但任何感谢任何帮助或指导。