使用骨干.js:
我有一个顶级ModelA,其中包含2个属性和2个嵌套模型ModelB和ModelC。ModelB和ModelC分别具有2个属性,如下所示:
ModelA
attributeA1
attributeA2
ModelB
attributeB1
attributeB2
ModelC
attributeC1
attributeC2
对于ModelA,有一个ViewA,对于ModelB,有一个ViewB。ViewA的render函数在主体上放置一个新的div,而ViewB的render创建一个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?
Q1:ViewB具有功能funcB1。此函数可以访问其父模型的属性吗?属性,例如attributeA1,甚至attributeC1(将是同级/表兄弟)?
Q2:作为对Q1的进一步扩展,funcB1可以访问与ViewA相关的DOM元素吗?(在此示例中,为#new div?)
问题3:通常,我如何如上所述定义视图和模型之间的关联,以便所有内容正确地结合在一起?
我意识到这个问题有点抽象,但是任何人都希望得到任何帮助或指导。