[EXT JS]"hasMany" association on ExtJS 4.1.1a
ExtJS uses "hasMany" association to support nested json.
However the sencha docs lacks well organized documents to guide how to use it. The coder has to test a lot to make their model work.
There is a post which pointed some extra "rules" to make the hasMany work.
http://extjs-tutorials.blogspot.co.uk/2012/05/extjs-hasmany-relationships-rules.html
In my case, I used Extjs 4.1.1a, after referencing a lot and testing a lot. I got my codes working.
I used hasMany twice, one for reading nested json from existing json file; one for loading json docs from mongodb database.
Model is defined in "model" folder in MVC architecture.
(1) Loading from mongoDB database
Ext.define('App.model.ItemPrice', {
extend : 'Ext.data.Model',
idProperty : '_id',
fields : [{
name : '_id',
type : 'string'
}, {
name : 'Category',
type : 'string'
}, {
name : 'SE',
type : 'string'
}, {
name : 'Version',
type : 'string'
}, {
name : 'Unit',
type : 'string'
}, {
name : 'Partname',
type : 'string'
}, {
name : 'OTC',
type : 'Number'
}, {
name : 'MC',
type : 'Number'
}, {
name : 'Currency',
type : 'string'
}],
hasMany : {
name : 'Attributes',
model : 'App.model.ItemAttribute',
associationKey : 'Attributes'
},
proxy : {
type : 'rest',
url : '/itemprices',
reader : {
type : 'json',
root : 'prices',
successProperty : 'success'
},
writer : Ext.create('Ext.data.writer.DeepJson')
}
});
Ext.define('App.model.ItemAttribute', {
extend : 'Ext.data.Model',
fields : [{
name : 'FieldKey',
type : 'string'
}, {
name : 'FieldValue',
type : 'string'
}]
});
Store defined:
Ext.define('App.store.ItemPrices', {
extend: 'Ext.data.Store',
autoLoad: false,
autoSync: false,
model: 'App.model.ItemPrice'
});
One thing needs to be noticed: The "name" and "associationKey" are recommended to be the same, and DO NOT use "field" to be their value. I used to spend 2 hours to find the error, at last I changed "field" to other word, it worked. So I suspected "field" is a keyword which can not be used as the value of "name" and "associationKey" in hasMany.
To read from database, we still need to write the webservice which connects the database and gives API to web client. I used Node.js here.
As pointed in Extjs proxy(see "App.model.ItemPrice"), The web service is "rest", to write this I used "express" module. To implement reading from MongoDB I used "Mongoose" module.
var express = require('express'),
app = module.exports = express();
// MongoDB
var mongoose = require('mongoose'),
db = mongoose.connect('mongodb://127.0.0.1/IaaSDBG2');
//create the price info Model using the 'pricecatalogue' collection as a data-source
var PriceCatalg = mongoose.model('pricecatalogue', new mongoose.Schema({
Category: String,
SE: String,
Version : String,
Unit: String,
Partname: String,
OTC: Number,
MC: Number,
Currency: String,
Attributes: [{
FieldKey: String,
FieldValue: String
}]
}));
// Configuration
app.configure(function () {
//app.set('views', __dirname + '/views');
//app.set('view engine', 'jade');
app.use(express.bodyParser());//parse JSON into objects
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/IaaSPriceTool'));
});
app.configure('development', function () {
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
app.configure('production', function () {
app.use(express.errorHandler());
});
// Routes
app.get('/', function (req, res) {
res.redirect('/index.html');
});
/*
PriceItem in database GET Express web service
*/
app.get('/itemprices', function (req, res) {
PriceCatalg.find({}, function (err, compntPrices) {
res.contentType('json');
res.json({
success: true,
prices: compntPrices
});
});
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", 3000, app.settings.env);
(2) Loading from file
Ext.define('App.model.AppConfig', {
extend : 'Ext.data.Model',
fields : [{
name : 'VERSION',
type : 'string'
}],
hasMany : [{
name : 'COMPONENTS',
model : 'App.model.CompntConfig',
associationKey : 'COMPONENTS'
}]
});
Ext.define('App.model.CompntConfig', {
extend : 'Ext.data.Model',
fields : [{
name : 'CATEGORY',
type : 'string'
},{
name : 'SE',
type : 'string'
},{
name : 'UNIT',
type : 'string'
}],
hasMany : [{
name : 'FIELDS',
model : 'App.model.FieldConfig',
associationKey : 'FIELDS'
}],
belongsTo: 'App.model.AppConfig'
});
Ext.define('App.model.FieldConfig', {
extend : 'Ext.data.Model',
fields : [{
name : 'NAME',
type : 'string'
}, {
name : 'FULLNAME',
type : 'string'
}],
belongsTo: 'App.model.CompntConfig'
});
The store is created dynamically in the Controller.
var ConfigStore = Ext.create('Ext.data.Store', {
model : 'App.model.AppConfig',
proxy : {
type : 'ajax',
url : "./PriceCatalgJson_V2.0.json",
reader : {
type : 'json'
}
}
});
[EXT JS]"hasMany" association on ExtJS 4.1.1a的更多相关文章
- sencha ext js 6 入门
Sencha Ext JS号称是目前世界上最先进和最强大的.支持多平台多设备的JavaScript应用程序开发框架.首先看一下Ext JS的发展简史. 1 Ext JS发展简史 YUI-Ext的作者J ...
- Ext Js详解指南
什么是Ext JS 走进Ext的世界 Ext JS是一款富客户端开发框架它基于javascript.HTML和CSS开发而成,无需安装任何插件即可在常用浏览器中创建出绚丽的页面效果. 个人总结Ext ...
- [Ext JS 4] Extjs 它 initComponent 和 constructor差分
initComponent 和 constructor是什么 Extjs 提供的组件还是挺丰富的, 可是有时候需求更丰富. 当Extjs 原生的组件无法实现我们的要求的时候, 就须要扩展Extjs 的 ...
- Ext JS 6学习文档–第1章–ExtJS入门指南
Ext JS 入门指南 前言 本来我是打算自己写一个系列的 ExtJS 6 学习笔记的,因为 ExtJS 6 目前的中文学习资料还很少.google 搜索资料时找到了一本国外牛人写的关于 ExtJS ...
- 【转载】《Ext JS 4 First Look》翻译之一:新特性
免责声明: 本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除. 原文作者:^_^肥仔John 原文地址:http://www.cnblogs. ...
- Ext.js基础
第一章:Ext.js基础 好书推荐 Javascript设计模式 征服ajax web 2.0开发技术详解 简介 基础要求 了解HTML.CSS.熟练JS.JS的OOP.AJAX JSP/PHP/AS ...
- Ext JS - 问答
Ext JS - 问答 在下面你将可以找到关于Ext JS 的最常见问题的答复.如果没有找到您所需的答复,请访问 Ext JS 论坛或者提交一个支持申请. 如果你确信你的问题可以对本页有补充,请让我们 ...
- 《Ext JS模板与组件基本知识框架图----模板》
最近在整理Ext JS的模板和组件,在参考<Ext JS权威指南>,<Ext JS Web应用程序开发指南>,<Ext JS API>等相关书籍后才写下这篇< ...
- CDH5X 安装oozie报错To enable Oozie web console install the Ext JS library.
最近在CDH5.X 安装oozie 服务,服务安装完毕,访问oozie server ui,报如下错误: 页面提示: Oozie web console is disabled.To enable O ...
随机推荐
- 【web前端开发】浏览器兼容性处理大全
1.居中问题 div里的内容,IE默认为居中,而FF默认为左对齐,可以尝试增加代码margin: 0 auto; 2.高度问题 两上下排列或嵌套的div,上面的div设置高度(height),如果di ...
- js如何处理字符串中带有↵字符
js或vue中如何处理字符串中带有↵字符 split('\n') 使用split('\n')将字符串分割成数组就行 如果我们在vue中,只需要在页面中绑定变量时操作split('\n')就可以了: & ...
- svn服务器 备份,迁移,部署方案
这次做业务迁移,要从一个云厂商迁移到某云厂商,之前每天到全备svn排到用场了,需要搭建一个全新到svn服务并要做迁移,并实现我们开发机到时时代码同步 一.svn备份有很多种,优劣都不同,百度可查,我采 ...
- qwe
这次作业我负责的部分是把爬取完的聊天记录经行数据挖掘以及经行各种普通过滤高级过滤等. 运行截图如下: 数据分为四部分:账户名.qq/邮箱.包含关键词的发言次数.包含关键词的发言字数. 遇到的困难及解决 ...
- C# Dsoframer.ocx 如何在winform中嵌入Excel,内嵌Excel,word
如果你还不太清楚Dspframer.ocx怎么放到窗体上就看上一篇文章,里面详细介绍了是如何放到窗体上的. 链接:http://www.cnblogs.com/pingming/p/4182045.h ...
- .net 简体转换繁体实例,繁体转换简体 Encode.dll、下载
在项目中先引用Encode.dll 下面是下载地址: Encode.dll ChineseConverter.dll 1.html页面代码 <%@ Page Language="C# ...
- TreeView的使用
用于显示多级层次关系 每一项是一个节点,也就是一个Node,是一个TreeNode节点,Nodes是该控件节点的集合. selectedNode用户选中的节点,如果没有选中则为null 1. 当选中后 ...
- ping traceroute原理
ping命令工作原理 ping命令主要是用于检测网络的连通性. Ping命令发送一个ICMP请求报文给目的IP,然后目的IP回复一个ICMP报文. 原理:网络上的机器都有唯一确定的IP地址,我们给目标 ...
- 网页添加提示音,用setInterval
如果一条数据通过审核了,修改数据库中一个值,用户怎么异步动态知道自己的记录通过审核了呢,我是通过音乐和提示的方式. 网页中添加如下代码: <style> #notify { positio ...
- 在@Async注解下RequestContextHolder.getRequestAttributes() 获得null的情况
我们有的时候会在service层获取request填充一些诸如用户名和IP地址等信息,这个时候如果不想从Controller层传request,可以在service直接使用 HttpServletRe ...