修改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'));
});
});
}
});
}
});
});
* @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上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端