您的位置:首页 > 其它

修改Model中hasMany中自动生成的属性值

2012-07-15 11:27 113 查看
在学习EXTJS的文档是,在测试《The Data Package》中的例子时,文中讲到Model中的hasMany自动生成的是一个Store对象的引用,如hasMany: 'Post',自动生成的是post()方法,实际上指向的Post的Store引用。自动生成的Store在向后台请求数据时的Get参数为:posts/?_dc=1342322365337&limit=25&page=1&start=0&filter=%5B%7B%22property%22%3A%22user_id%22%2C%22value%22%3A1%7D%5D,其中limit的值为25,这是默认值,如果想要修改这个默认值,可以调用:user.posts().proxy.setExtraParam("limit", 100);进行修改。完整代码如下:

/**
* @example Lazy Associations
*
* This example demonstrates lazy loading of a {@link Ext.data.Model}'s associations only when requested.
* a `User` model is loaded, then a separate request is made for the `User`'s associated `Post`s
* See console for output.
*/

// define the User model
Ext.define('User', {
extend: 'Ext.data.Model',
fields: ['id', 'name', 'age', 'gender'],

proxy: {
type: 'rest',
url : 'data/users',
reader: {
type: 'json',
root: 'users'
}
},

hasMany: 'Post' // shorthand for {model: 'Post', name: 'posts'}
});

//define the Post model
Ext.define('Post', {
extend: 'Ext.data.Model',
fields: ['id', 'user_id', 'title', 'body'],

proxy: {
type: 'rest',
url : 'data/posts',
reader: {
type: 'json',
root: 'posts'
}
},

belongsTo: 'User',
hasMany: {model: 'Comment', name: 'comments'}
});

//define the Comment model
Ext.define('Comment', {
extend: 'Ext.data.Model',
fields: ['id', 'post_id', 'name', 'message'],

belongsTo: 'Post'
});

Ext.require('Ext.data.Store');
Ext.onReady(function () {
// Loads User with ID 1 User's Proxy
User.load(1, {
success: function (user) {
console.log("User: " + user.get('name'));

// Loads posts for user 1 using Post's Proxy
user.posts().proxy.setExtraParam("limit", 100);
user.posts().load({
callback: function (posts, operation) {
Ext.each(posts, function (post) {
console.log("Comments for post: " + post.get('title'));

post.comments().each(function (comment) {
console.log(comment.get('message'));
});
});
}
});
}
});
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐