在sencha touch中将单个对象加载到数据存储中

时间:2022-01-21 16:44:40

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 this entry,

它不起作用。看完这个条目后,

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.

BTW - 我的服务器端代码在Ruby on Rails中。

4 个解决方案

#1


9  

Animal from the Ext Development Team has given this very useful solution:

Ext Development Team的动物提供了这个非常有用的解决方案:

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.

通过简单地以root身份使用函数,您可以确保读者获取数组中的对象。

#2


5  

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)

这样我仍然可以保留一个宁静的API,并将Sencha的json表示视为表示资源的另一种方式(即json / xml / yaml)

#3


2  

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...

使用商店时,您总是必须发送一个数组,即使它只有一个记录。我不知道为什么总是发送一个数组在你的应用程序中有问题,但它应该简单,只需强制参数为数组...

persons = [*persons]

#4


0  

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样式的服务器端和客户端代码。

#1


9  

Animal from the Ext Development Team has given this very useful solution:

Ext Development Team的动物提供了这个非常有用的解决方案:

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.

通过简单地以root身份使用函数,您可以确保读者获取数组中的对象。

#2


5  

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)

这样我仍然可以保留一个宁静的API,并将Sencha的json表示视为表示资源的另一种方式(即json / xml / yaml)

#3


2  

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...

使用商店时,您总是必须发送一个数组,即使它只有一个记录。我不知道为什么总是发送一个数组在你的应用程序中有问题,但它应该简单,只需强制参数为数组...

persons = [*persons]

#4


0  

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样式的服务器端和客户端代码。