javascript 在 sencha touch 中将单个对象加载到数据存储中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5791684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
load single object into a data store in sencha touch
提问by Rubinsh
I'm trying to load a single json object using sencha touch. When I work with arrays, all is well, but I havn't find a way of loading a single object into an Ext.data.Store
我正在尝试使用 sencha touch 加载单个 json 对象。当我使用数组时,一切都很好,但我还没有找到将单个对象加载到 Ext.data.Store 的方法
here is a sample of what I'm trying to load:
这是我尝试加载的示例:
{"person":{"name":"John","surname":"Fox"}}
and it's not working. after looking at thisentry,
它不起作用。看完这篇文章后,
I tried to load the following and it worked:
我尝试加载以下内容并且它起作用了:
[{"person":{"name":"John","surname":"Fox"}}]
My questions is: is there a way of loading it w/o the [ ]? I had to modify my server side code in order to do this and it feels to me like a code smell... I want to be able to load a single json object w/o putting it into a list.
我的问题是:有没有办法在没有 [] 的情况下加载它?为了做到这一点,我不得不修改我的服务器端代码,这对我来说就像一种代码气味……我希望能够加载一个 json 对象,而无需将其放入列表中。
here is my Sencha Touch proxy code:
这是我的 Sencha Touch 代理代码:
Ext.regModel("Person", {
fields: ['name','surname']
});
var store = new Ext.data.Store({
model : "Person",
autoLoad : true,
proxy: {
type: 'ajax',
url : 'my json url...',
reader: {
type: 'json',
record: 'person'
}
}
});
BTW - my server side code is in Ruby on Rails.
顺便说一句 - 我的服务器端代码是在 Ruby on Rails 中。
回答by Olivier - interfaSys
Animal from the Ext Development Team has given this very useful solution:
来自 Ext 开发团队的 Animal 给出了这个非常有用的解决方案:
reader: {
type: 'json',
root: function(data) {
if (data.users) {
if (data.users instanceof Array) {
return data.users;
} else {
return [data.users];
}
}
}
}
By simply using a function as root, you can make sure the reader gets the object in an array.
通过简单地将函数用作根,您可以确保读取器获取数组中的对象。
回答by Rubinsh
update: the way to access a single json object using sencha is just to make a simple request and not use a data store. here is an example of code that does this:
更新:使用 sencha 访问单个 json 对象的方法只是发出一个简单的请求,而不是使用数据存储。这是执行此操作的代码示例:
Ext.ns('myNS');
Ext.Ajax.request({
url: 'my url',
method: 'GET',
params: {
someParam: 'someValue'
},
success: function(result, request) {
var json = Ext.decode(result.responseText);
myNS.loadedPerson = json.person;
},
failure: function(result, request) {
Ext.Msg.alert('Error!', 'There was a problem while loading the data...');
}
});
If you still insist on using a dataStore - here is a possible solution in the server side that maintains a restful API: here is what I did in order to keep my API restful:
如果您仍然坚持使用 dataStore - 这里是在服务器端维护一个宁静的 API 的一个可能的解决方案:这是我为了保持我的 API 宁静所做的:
I added a new MIME type called sencha, that behaves exactly like json but it also wraps the json in [ ] parenthesis.
我添加了一个名为 sencha 的新 MIME 类型,它的行为与 json 完全一样,但它也将 json 包装在 [ ] 括号中。
It might be (and probably is) an overkill, but this way it's not intrusive to my JSON API. here is the code for the new MIME type:
这可能(也可能是)矫枉过正,但这样它就不会侵入我的 JSON API。这是新 MIME 类型的代码:
Mime::Type.register_alias "application/json", :sencha
#Add the renderer, and register a responder:
require 'action_controller/metal/renderers'
require 'action_controller/metal/responder'
# This is also how Rails internally implements its :json and :xml renderers
# Rarely used, nevertheless public API
ActionController::Renderers.add :sencha do |json, options|
json = ActiveSupport::JSON.encode(json) unless json.respond_to?(:to_str)
# Add [ ] around the response
json = "[#{json}]"
json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
self.content_type ||= Mime::JSON
self.response_body = json
end
# This sets up a default render call for when you do
# respond_to do |format|
# format.sencha
# end
class ActionController::Responder
def to_sencha
controller.render :sencha => resource
end
end
this allows me to define the following proxy (notice the format: 'sencha' ) in the client side:
这允许我在客户端定义以下代理(注意格式:'sencha'):
proxy: {
type: 'rest',
format: 'sencha',
url : my server url,
reader: {
type: 'json',
record: 'person'
}
}
This way I can still keep a restful API, and regard Sencha's json representation as another way of representing resources (i.e. json/xml/yaml)
这样我仍然可以保持一个restful API,并将Sencha的json表示作为另一种表示资源的方式(即json/xml/yaml)
回答by iwiznia
Not sure if it's what you want, but you can load it through the model like:
不确定它是否是您想要的,但您可以通过模型加载它,例如:
var oneperson = new Person({id: anId});
When using a store you always have to send an array, even if it only has one record in it. I don't know why always sending an array is problematic in your application, but it should be prette simple, just force the parameter to be an array...
使用 store 时,您始终必须发送一个数组,即使其中只有一条记录。我不知道为什么在您的应用程序中总是发送数组是有问题的,但它应该很简单,只需强制参数为数组...
persons = [*persons]
回答by bor1s
I agree with iwiznia, Ext.data.Store always waits for a [...] response, and probably you should change serverside code to return array of persons.
Maybe you should also try to use REST style intraction of your serverside and client code.
我同意iwiznia, Ext.data.Store 总是等待 [...] 响应,并且可能您应该更改服务器端代码以返回人员数组。
也许您还应该尝试使用服务器端和客户端代码的 REST 风格内嵌。