修改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'));
                        });
                    });
                }
            });
        }
    });
});
复制代码
posted @   吴东雷  阅读(1905)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示