1. #1
    Sencha User
    Join Date
    Jun 2009
    Posts
    37
    Vote Rating
    0
    NeoVanGoth is on a distinguished road

      0  

    Lightbulb Hint: Models with hasMany in Lists

    Hint: Models with hasMany in Lists


    Hi,

    it took me several hours to fiddle this out.

    I had a model with a hasMany association and tried to use it in a XTemplate of a list. To be precise, I had a list of persons, where every person could have many places of work and wanted to display all places below the persons name in the list.
    After following the API docs and the examples for Model and XTemplate, I couldn't get it to work.

    I had something like this (simplified):
    Code:
    Models
    
    Ext.regModel ('PlaceOfWork', {
      fields: [
        { name: 'name', type: 'string' }
      ],
      belongsTo: 'Person'
    });
    
    Ext.regModel ('Person', {
      fields: [
        { name: 'firstname', type: 'string' },
        { name: 'lastname', type: 'string' }
      ],
      associations: [
        { type: 'hasMany', model: 'PlaceOfWork', name: 'placesOfWork' }
    });
    
    XTemplate
    
    new Ext.Xtemplate ('<tpl for="."><div class="person">' +
      '  {firstname} <strong>{lastname}</strong>' +
      '  <tpl for="placesOfWork"><br /><small>{name}</small></tpl>' +
      '</div></tpl>');
    
    JSON Example
    
    [{"firstname":"Foo","lastname":"Bar","placesOfWork":[{name:"Laber"},{name:"Fasel"}]}]
    This produced the error "placesOfWork" is undefined. After doing some debugging, I found out that the template doesn't receive all keys in my JSON, but only those which are a field in the model.

    So remember: When using a hasMany association, it is _required_ to add the associations name as field!

    Code:
    Ext.regModel ('Person', {
      fields: [
        { name: 'firstname', type: 'string' },
        { name: 'lastname', type: 'string' },
        { name: 'placesOfWork', type: 'array' }
      ],
      associations: [
        { type: 'hasMany', model: 'PlaceOfWork', name: 'placesOfWork' }
    });
    I think, we need a far more complete example in the docs, as XTemplate in combination with Models seem to be quite important in S/Touch.

    [Edit: Typo]

  2. #2
    Sencha - Community Support Team edspencer's Avatar
    Join Date
    Jan 2009
    Location
    Palo Alto, California
    Posts
    1,939
    Vote Rating
    7
    edspencer is a jewel in the rough edspencer is a jewel in the rough edspencer is a jewel in the rough

      0  

    Default


    This got a lot easier in 0.98 - I added a comment with some explanation on another thread. I'm working on updating the API docs to make this clearer.
    Ext JS Senior Software Architect
    Personal Blog: http://edspencer.net
    Twitter: http://twitter.com/edspencer
    Github: http://github.com/edspencer

  3. #3
    Sencha User
    Join Date
    Sep 2010
    Location
    Amsterdam
    Posts
    22
    Vote Rating
    2
    savelee is on a distinguished road

      0  

    Default


    Thanks, you made my day!

  4. #4
    Touch Premium Member
    Join Date
    May 2011
    Location
    Norfolk & DC
    Posts
    100
    Vote Rating
    5
    epiphanydigital is on a distinguished road

      0  

    Default


    Hi guys... this is still not very intuitive to me.

    I have a models "Profiles" hasMany "Supplements". I open up my app and adding a profile works just fine, but the Supplement list tries to load the parent, "Profiles", info instead of the list of child records. That said, if I print out something like this on the Supplements list panel:

    Ext.Msg.alert('Warning!','There are ' + records.length + ' records.',Ext.emptyFn);

    I do get the correct number of records showing for Supplements.

    Thanks in advance for your help.
    twitter.com/epiphanydigital #sencha #drupal #jquery #craftbeer #guitar #photography

Similar Threads

  1. models inside models
    By kortovos in forum Sencha Touch 1.x: Discussion
    Replies: 2
    Last Post: 27 Sep 2010, 3:43 AM
  2. Questions about Models, Stores, and dynamic Lists
    By jonathanstark in forum Sencha Touch 1.x: Discussion
    Replies: 2
    Last Post: 10 Aug 2010, 8:54 AM

Thread Participants: 3

film izle

hd film izle

film sitesi

takipci kazanma sitesi

takipci kazanma sitesi

güzel olan herşey

takipci alma sitesi

komik eğlenceli videolar