1. #1
    Sencha User
    Join Date
    Mar 2014
    Posts
    103
    Vote Rating
    -6
    Answers
    5
    franva is infamous around these parts franva is infamous around these parts

      1  

    Default Answered: how to connect a model which is not full-mapping to data source to a store

    Dear friends,

    I have a xml data source which contains many nodes, but I am only interested in a few of nodes.

    Code:
    <config generated_at="2014-04-20 17:40:24">
        <nmts replicate="1" showPortables="1" tagfields="type,metric,starttime,endtime,maxtime,duration" visible="visible">
            <download_queue enable="0" />
            <locations name="Keilor East" download_noise_data="1" latitude="-37.73228" locid="3" longitude="144.86900" monitor_location_id="318" monitor_name="mel3" status="active" tagOrientation="SE" type="fixed" x="2857.2" y="23539.3" />
            <locations name="Bulla" download_noise_data="1" latitude="-37.60686" locid="2" longitude="144.82314" monitor_location_id="320" monitor_name="mel2" status="active" tagOrientation="SE" type="fixed" x="-2247.9" y="41177.8" />
            <locations name="Coolaroo" download_noise_data="1" latitude="-37.66640" locid="6" longitude="144.92731" monitor_location_id="342" monitor_name="mel6" status="active" tagOrientation="SE" type="fixed" x="9348.3" y="32808.1" />
            <locations name="Essendon" download_noise_data="1" latitude="-37.73880" locid="4" longitude="144.90410" monitor_location_id="343" monitor_name="mel4" status="active" tagOrientation="SE" type="fixed" x="6764.6" y="22621.5" />
            <locations name="Thomastown Fixed" download_noise_data="1" latitude="-37.67085" locid="61" longitude="144.99731" monitor_location_id="344" monitor_name="mel5" status="active" tagOrientation="SE" type="fixed" x="17140.6" y="32182.2" />
            <locations name="Keilor Bonfield Res" download_noise_data="1" latitude="-37.72383" locid="60" longitude="144.83806" monitor_location_id="346" monitor_name="asa31" status="active" tagOrientation="SE" type="portable" x="-587" y="24728.6" />
        </nmts>
    </config>
    I , for example, only want to retrieve "locations" which are in "nmts" node.

    Here is my code

    Code:
    Ext.define('myApp.store.myStore', {
        extend: 'Ext.data.Store',
        model:'myApp.model.myModel',
        storeId: 'myStore',
        autoLoad: true,
    
    
        proxy: {
            type: 'ajax',
            url: 'http://webtrak.bksv.com/mel/configuration',
            reader: {
                type: 'xml',
                rootProperty: 'nmts',
                record:'locations'
            }
        }
    });
    and here is my model
    Code:
    Ext.define('myApp.model.myModel', {
        extend: 'Ext.data.Model',
        config: {
            fields: [
                { name: 'name', type: 'string' },
                { name: 'lat', type: 'float' },
                { name: 'lng', type: 'float' },
                { name: 'locid', type: 'string' },
                { name: 'monitor_location_id', type: 'int' },
                { name: 'monitor_name', type: 'string' },
                { name: 'status', type: 'string' },
                { name: 'tagOrientation', type: 'string' },
                { name: 'stationType', type: 'string' },
                { name: 'x', type: 'float' },
                { name: 'y', type: 'float' }
    
    
            ]
        }
    });
    You can see that my model only have a part of attributes from the data source.

    and since the data source is on remote server I cannot test it in browsers, but when testing on devices, I cannot get any error feedback so that's the reason of making debugging impossible for me.

    I am not sure whether I write the code in a right way, please point out my errors if there are any.

    Thank you so much.

  2. I stand corrected. Attributes can be parsed using the field mapping config.
    http://docs.sencha.com/touch/2.3.1/#...ld-cfg-mapping
    Code:
    <m-par>
        <m-first age='50'>Joe</m-first>
        <m-child>
            <m-last>Shmoe</m-last>
        </m-child>
    </m-par>
    To parse the age attribute you would set up your model as follows
    Code:
    Ext.define("Fiddle.model.Parentt", {
        extend: "Ext.data.Model",
        config: {
            fields: [{
                name: 'first',
                type: 'string',
                mapping: 'm-first'
            }, {
                name: 'age',
                mapping: 'm-first @age'
            }],
            proxy: {
                type: 'ajax',
                url: 'data.xml',
                reader: {
                    type: 'xml',
                    record: 'm-par'
                }
            }
        }
    });

  3. #2
    Sencha User
    Join Date
    Mar 2014
    Posts
    103
    Vote Rating
    -6
    Answers
    5
    franva is infamous around these parts franva is infamous around these parts

      -1  

    Default

    hello~~~~~echo~~~~~~echo~~~~echo~~~~

  4. #3
    Sencha - Support Team
    Join Date
    Oct 2011
    Posts
    4,012
    Vote Rating
    134
    Answers
    338
    fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all

      0  

    Default

    Its hard to tell. Can you format your data using an xml beautifier and edit your original post.
    Here are some options for debugging on devices:
    Android:
    https://www.jshybugger.com/#!/

    Windows Phone:
    http://blogs.windows.com/windows_pho...th-weinre.aspx

    iOS
    http://phonegap-tips.com/articles/de...inspector.html
    Get more personalized support via a support subscription.

    Want to learn Sencha Touch 2? Check out
    Sencha training
    Sencha Touch in Action

    Need more help with your app? Hire Sencha Services services@sencha.com

    When posting code, please use BBCode's CODE tags.

    Need to include a test case use:
    The official Sencha Fiddle

  5. #4
    Sencha User
    Join Date
    Mar 2014
    Posts
    103
    Vote Rating
    -6
    Answers
    5
    franva is infamous around these parts franva is infamous around these parts

      1  

    Default Reply to you

    Hi, thank you for your reply.

    I have updated my xml code(I tried html wrapper, but after saved, the content within html tag became messy. code tag works, but not perfectly as you can see it)

    I would appreciate if you could explain me how SenCha Store bind retrieved data to the appointed model.
    Then I might be able to figure out how to do my job.

    Thank you

  6. #5
    Sencha - Support Team
    Join Date
    Oct 2011
    Posts
    4,012
    Vote Rating
    134
    Answers
    338
    fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all

      0  
    Get more personalized support via a support subscription.

    Want to learn Sencha Touch 2? Check out
    Sencha training
    Sencha Touch in Action

    Need more help with your app? Hire Sencha Services services@sencha.com

    When posting code, please use BBCode's CODE tags.

    Need to include a test case use:
    The official Sencha Fiddle

  7. #6
    Sencha User
    Join Date
    Mar 2014
    Posts
    103
    Vote Rating
    -6
    Answers
    5
    franva is infamous around these parts franva is infamous around these parts

      1  

    Default

    Quote Originally Posted by fmoseley View Post
    Hi fmoseley,

    Thank you for your reply.

    The example demonstrates retrieving values from xml elements, but in my case all values are attributes within elements.

    Here is the xml from the data source


    HTML Code:
    <nmts replicate="1" showPortables="1" tagfields="type,metric,starttime,endtime,maxtime,duration" visible="visible">
          <download_queue enable="0"/>
          <locations name="Keilor East" download_noise_data="1" latitude="-37.73228" locid="3" longitude="144.86900"monitor_location_id="318" monitor_name="mel3" status="active" tagOrientation="SE" type="fixed" x="2857.2" y="23539.3"/>
          <locations name="Bulla" download_noise_data="1" latitude="-37.60686" locid="2" longitude="144.82314" monitor_location_id="320"monitor_name="mel2" status="active" tagOrientation="SE" type="fixed" x="-2247.9" y="41177.8"/>
          <locations name="Coolaroo" download_noise_data="1" latitude="-37.66640" locid="6" longitude="144.92731"monitor_location_id="342" monitor_name="mel6" status="active" tagOrientation="SE" type="fixed" x="9348.3" y="32808.1"/>
          <locations name="Essendon" download_noise_data="1" latitude="-37.73880" locid="4" longitude="144.90410"monitor_location_id="343" monitor_name="mel4" status="active" tagOrientation="SE" type="fixed" x="6764.6" y="22621.5"/>
          <locations name="Thomastown Fixed" download_noise_data="1" latitude="-37.67085" locid="61" longitude="144.99731"monitor_location_id="344" monitor_name="mel5" status="active" tagOrientation="SE" type="fixed" x="17140.6" y="32182.2"/>
          <locations name="Keilor Bonfield Res" download_noise_data="1" latitude="-37.72383" locid="60" longitude="144.83806"monitor_location_id="346" monitor_name="asa31" status="active" tagOrientation="SE" type="portable" x="-587" y="24728.6"/>
    </nmts>



    And here is the xml format in the example code.

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?> 
    *    <users> 
    *         <user> 
    *             <id>1</id> 
    *             <name>Ed Spencer</name> 
    *             <email>ed@sencha.com</email> 
    *         </user> 
    *         <user> 
    *             <id>2</id>
     *             <name>Abe Elias</name> 
    *             <email>abe@sencha.com</email> 
    *         </user> 
    *     </users>


    And here is my store.

    Code:
    Ext.define('myapp.store.myStore', {
        extend: 'Ext.data.Store',
        models: ['myapp.model.myModel'],
        storeId: 'SensorStore',
        autoLoad: true,
        proxy: {
            type: 'ajax',
            model: 'myapp.model.myModel',
            url: 'http://webtrak.bksv.com/mel/configuration',
            reader: {
                type: 'xml',
                record: 'locations',
                rootProperty: 'nmts'
            }
        }
    });
    I am not interested in all values for each of locations, so in my model, I do not have all properties to each of the attributes from the xml data source. I am not sure whether this is another reason which causes my app doesn't work.

    Please help, thanks

  8. #7
    Sencha - Support Team
    Join Date
    Oct 2011
    Posts
    4,012
    Vote Rating
    134
    Answers
    338
    fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all

      0  

    Default

    In that case you will have to create a custom xml reader class to parse your data. You could also do a ajax request to retrieve your data and create a javascript function parse your data and add it to your store.
    Get more personalized support via a support subscription.

    Want to learn Sencha Touch 2? Check out
    Sencha training
    Sencha Touch in Action

    Need more help with your app? Hire Sencha Services services@sencha.com

    When posting code, please use BBCode's CODE tags.

    Need to include a test case use:
    The official Sencha Fiddle

  9. #8
    Sencha User
    Join Date
    Mar 2014
    Posts
    103
    Vote Rating
    -6
    Answers
    5
    franva is infamous around these parts franva is infamous around these parts

      0  

    Default Reply to you

    Quote Originally Posted by fmoseley View Post
    In that case you will have to create a custom xml reader class to parse your data. You could also do a ajax request to retrieve your data and create a javascript function parse your data and add it to your store.
    Hi fmoseley,
    Thank you for your idea, could you please provide me a demo code?
    As JavaScript for Sencha Touch doesn't have Intellisence like C#, so I always find it's really hard to know what attributes an object have.

    Thank you.

  10. #9
    Sencha - Support Team
    Join Date
    Oct 2011
    Posts
    4,012
    Vote Rating
    134
    Answers
    338
    fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all

      0  

    Default

    console.log and debugger statements are your friend. I would recommend examining the code for the Ext.data.reader.Xml class. It offers the best example of what you need to do. You just have to tweak the code to pull data from the attributes.
    http://docs.sencha.com/touch/2.3.1/s...ata-reader-Xml
    Get more personalized support via a support subscription.

    Want to learn Sencha Touch 2? Check out
    Sencha training
    Sencha Touch in Action

    Need more help with your app? Hire Sencha Services services@sencha.com

    When posting code, please use BBCode's CODE tags.

    Need to include a test case use:
    The official Sencha Fiddle

  11. #10
    Sencha - Support Team
    Join Date
    Oct 2011
    Posts
    4,012
    Vote Rating
    134
    Answers
    338
    fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all fmoseley is a name known to all

      0  

    Default

    I stand corrected. Attributes can be parsed using the field mapping config.
    http://docs.sencha.com/touch/2.3.1/#...ld-cfg-mapping
    Code:
    <m-par>
        <m-first age='50'>Joe</m-first>
        <m-child>
            <m-last>Shmoe</m-last>
        </m-child>
    </m-par>
    To parse the age attribute you would set up your model as follows
    Code:
    Ext.define("Fiddle.model.Parentt", {
        extend: "Ext.data.Model",
        config: {
            fields: [{
                name: 'first',
                type: 'string',
                mapping: 'm-first'
            }, {
                name: 'age',
                mapping: 'm-first @age'
            }],
            proxy: {
                type: 'ajax',
                url: 'data.xml',
                reader: {
                    type: 'xml',
                    record: 'm-par'
                }
            }
        }
    });
    Get more personalized support via a support subscription.

    Want to learn Sencha Touch 2? Check out
    Sencha training
    Sencha Touch in Action

    Need more help with your app? Hire Sencha Services services@sencha.com

    When posting code, please use BBCode's CODE tags.

    Need to include a test case use:
    The official Sencha Fiddle

Thread Participants: 1