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

在学习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'));
                        });
                    });
                }
            });
        }
    });
});

转载于:https://www.cnblogs.com/fxwdl/archive/2012/07/15/2592164.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值