extjs 4.2 中的关联示例: [英] Association Sample in extjs 4.2:
本文介绍了extjs 4.2 中的关联示例:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能给我指出一个 ExtJS 中关联的工作示例(与 hasMany 和belongsTo).请不要将我指向 Sencha 文档或任何与 Sencha 相关的示例,因为我几乎尝试了所有方法,但没有一个有效......
Can any one please point me to a working example of association (with hasMany and belongsTo) in ExtJS. Please don't point me to Sencha docs or any examples related to Sencha because I had tried almost everything but none of them works...
推荐答案
运行示例(打开浏览器控制台):
Running sample (turn on your browser console):
Ext.define('My.model.Author', {
extend:'Ext.data.Model',
fields:[
'name'
]
});
Ext.define('My.model.Comment', {
extend:'Ext.data.Model',
fields:[
'emailAddress',
'body'
]
});
Ext.define('My.model.BlogPost', {
extend:'Ext.data.Model',
fields:[
'title',
'body'
],
belongsTo:[
{
name:'author',
instanceName:'author',
model:'My.model.Author',
getterName:'getAuthor',
setterName:'setAuthor',
associationKey:'author'
}
],
hasMany:[
{
name:'comments',
model:'My.model.Comment',
associationKey:'comments'
}
],
proxy:{
type:'ajax',
url:'https://dl.dropboxusercontent.com/u/1015920/Ext/blog-posts.json',
reader:{
type:'json',
root:'data'
}
}
});
My.model.BlogPost.load(1, {
success:function(record, operation){
console.log(record.get('title')); // "some title"
console.log(record.getAuthor().get('name')); // "neil"
console.log(record.comments().getCount()); // 2
}
});
在此处阅读更多信息:
http://extjs-tutorials.blogspot.ca/2012/05/extjs-belongsto-association-rules.html
http://extjs-tutorials.blogspot.ca/2012/05/extjs-hasmany-relationships-rules.html
使用的样本数据:
{
"data": [
{
"id": 1,
"title": "some title",
"body": "some body",
"author": {"id":1, "name": "neil"},
"comments": [
{
"id":55,
"emailAddress": "user@example.com",
"body": "test comment"
},
{
"id":66,
"emailAddress": "user2@example.com",
"body": "another comment"
}
]
}
]
}
这篇关于extjs 4.2 中的关联示例:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文