惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
Last Week in AI
Last Week in AI
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
小众软件
小众软件
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
WordPress大学
WordPress大学
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
罗磊的独立博客
The Cloudflare Blog
V
V2EX
月光博客
月光博客
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
博客园 - 【当耐特】
T
Tailwind CSS Blog

博客园 - 吴东雷

配置NuGet自动下载丢失的Dll SharePoint2010站点在IE下的操作异常 ExtJS的store.sync向Asp.net MVC的Action提交时引发System.Reflection.AmbiguousMatchException异常 ReportViewerWebWebpart显示报表时显示的脚本错误:Uncaught Sys.ArgumentNullException: Sys.ArgumentNullException: 值不能为 null。参数名: panelsCreated[0] 【转载】javascript getComputedStyle,getPropertyValue,CurrentStyle说明 【原创】关于SPSecurity.RunWithElevatedPrivileges的一个问题[A problem about SPSecurity.RunWithElevatedPrivileges] 【原创】如何让SharePoint2010的内联代码支持.Net framework 3.5[How to embed inline code in aspx with .net framework 3.5 syntax] 【半原创】SharePoint CAML 查询生成器[SharePoint 2010 CAML Query Builder] 【原创】在DataFormWebPart中将列表附件显示为图片(二)[How to display list item attachments as image in DFWP Part 2] 【原创】如何在DataFormWebPart中嵌入自定义控件[How to embeded custom control in DFWP] 【原创】SharePoint2010的SaveButton如何实现在跳转前给用户以提示[How to implement SaveButton display an alert when Redirect action] 【原创】在DataFormWebPart中将列表附件显示为图片(一)[How to display list item attachments as image in DFWP Part1] Asp.net中全局缓存的几种方式 ExtJS中自定义组件并将事件暴露给调用者 从控制台测试SharePoint遇到的两个问题 The Sencha Class System(理解ExtJs定义类的过程) IIS7中Sites(站点), Applications(应用程序), Virtual Directories(虚拟目录)的粗浅理解 按钮的ajax请求时,一次点击两次提交的问题 MVC3中Razor模板引擎如何修改View的基类
修改Model中hasMany中自动生成的属性值
吴东雷 · 2012-07-15 · via 博客园 - 吴东雷

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

一点说明:为什么在标题中要嵌入英文?原因是为了能够让国外的网友能查询到这篇文章。平常在Google上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。