[英]Cannot parse local json
I am just starting out with Sencha Touch and working on the version 2.2.1. 我刚开始使用Sencha Touch并正在开发2.2.1版本。 For some reason, I cannot get my local json to parse properly.
由于某种原因,我无法正确解析我的本地json。 I know it is not the response problem because I can see the json in my chrome developers tool.
我知道这不是响应问题,因为我可以在chrome开发人员工具中看到json。
Here is my store
这是我的
store
Ext.define('MyApp.store.Tasks', {
extend: 'Ext.data.Store',
requires: [
'MyApp.model.Task'
],
config: {
autoLoad: true,
storeId: 'tasksStore',
model: 'MyApp.model.Task',
proxy: {
type: 'ajax',
url: 'tasks.json',
reader: {
type: 'json',
rootProperty: 'tasks'
}
}
}
});
Here's my Model
这是我的
Model
Ext.define('MyApp.model.Task', {
extend: 'Ext.data.Model',
config: {
fields: [
{ name: 'id', type: 'int' },
{ name: 'task', type: 'string', defaultValue: 'task' }
]
}
});
I am using Jasmine
to test my store. 我正在使用
Jasmine
来测试我的商店。 Here's my spec 这是我的规格
describe('MyApp.store.Tasks', function() {
it('Number of tasks should be four', function() {
var store = Ext.create('MyApp.store.Tasks');
expect(store.getCount()).toBe(4);
});
});
And, here's my sample json
file. 而且,这是我的示例
json
文件。 It is in the same directory as Sencha's index.html
file, which is the root directory. 它与Sencha的
index.html
文件在同一目录中,该文件是根目录。
{
"tasks":[
{
"task":"Some Product",
"id":0
},
{
"task":"Another Product",
"id":1
},
{
"task":"A third product",
"id":2
},
{
"task":"A fourth product",
"id":3
}
]
}
Is it because of instantiation issues? 是因为实例化问题吗? Or am I missing some critical piece in here?
还是我在这里错过了一些重要的作品? I tried jsonp for proxy type, but it needs a wrapper around the response and I don't quite know how to do it.
我尝试将jsonp用于代理类型,但是它需要响应周围的包装,而且我不太清楚该怎么做。 I am testing on both Safari and Chrome, unfortunately the unit test fails on both browsers.
我正在Safari和Chrome上进行测试,很遗憾,两种浏览器上的单元测试均失败。
Thanks! 谢谢!
Store loading is asynchronous, so you cannot access their data immediately after having created them. 存储加载是异步的,因此您无法在创建它们后立即访问它们的数据。
To know when the store has been loaded, you can listen on the load
event of the store: 要知道何时加载商店,可以侦听商店的
load
事件 :
var store = Ext.create('MyApp.store.Tasks');
// you cannot use the store's data yet
// expect(store.getCount()).toBe(4);
store.on('load', function(records, success) {
if (success) {
// here the store has been loaded
expect(store.getCount()).toBe(4);
}
});
Or, you can also pass a callback to the load
method : 或者,您也可以将回调传递给
load
方法 :
var store = Ext.create('MyApp.store.Tasks', {autoLoad: false});
store.load({
callback: function(records, operation, success) {
if (success) {
// here the store has been loaded
expect(store.getCount()).toBe(4);
}
}
});
Now, that means that you'll have to make your Jasmine test asynchronous as well: 现在,这意味着您还必须使Jasmine测试异步 :
describe('MyApp.store.Tasks', function() {
it('Number of tasks should be four', function() {
var result = null,
store = Ext.create('MyApp.store.Tasks');
store.on('load', function(store, records, success) {
result = success;
});
// using jasmine async...
waitsFor(function() {
return result !== null;
});
// this functin will be executed when the one in waitsFor returns true
runs(function() {
expect(store.getCount()).toBe(4);
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.