Results 1 to 2 of 2

Thread: ViewModel Links with extra params.

  1. #1

    Default ViewModel Links with extra params.

    Kind of a follow up question regarding: https://www.sencha.com/forum/showthr...elds-in-a-View

    One way seems to be to have a single record bind to the ViewModel instead of a store. But another way seems to use the Links in a ViewModel.

    Currently working on a CRM where a Relation has to be loaded in and shown in the fields of the View.
    Code:
    Ext.define('Crm.view.relation.RelationModel', {    
        extend: 'Ext.app.ViewModel',
        alias: 'viewmodel.relation-relation',
    
        requires:[
            'Crm.model.Relation'
        ],
    
        links: {
            relation: {
                type: 'PdcCrm.model.Relation',
                id: 100 //However this would need to be filled dynamically  
            }
        }
    
    });
    Code:
    Ext.define('Crm.model.Relation', {    extend: 'Ext.data.Model',
    
        idProperty: 'id',
    
        fields: [
            { name: 'id',           type: 'string'},
            { name: 'name',         type: 'string'},
            { name: 'category',     type: 'string'},
            { name: 'email',        type: 'string'},
            { name: 'address',      type: 'string'},
            { name: 'zipcode',      type: 'string'},        
            { name: 'location',     type: 'string'},
            { name: 'countryname',  type: 'string'},
            { name: 'countrycode',  type: 'string'},
            { name: 'teltapi',      type: 'string'},
            { name: 'telcountrynr', type: 'string'},
            { name: 'telkennr',     type: 'string'},
            { name: 'telabnnr',     type: 'string'},
            { name: 'registered',   type: 'boolean'}    
        ],
    
        proxy: {
            type:           'rest',
            appendId : false,
           url:'url to the application server',
            reader:         {type: 'json', rootProperty: 'data'}
        }
    
    });
    But this only allows you to use an ID as parameter and nothing else. So what if I want to get this Relation but als Register it within the same request? Isn't there any way to add extra params in the request?

    I'm aware of the extraParams property in a Proxy but you can't really access that in this case.

  2. #2
    Sencha User
    Join Date
    Sep 2007
    Location
    Phoenix AZ
    Posts
    118
    Answers
    5

    Default

    I think what you are looking to do is more along the lines of the relation in the schema/model.

    https://docs.sencha.com/extjs/7.0.0/...a_package.html

    Take a look at the data package... see link above.

    This allows you to do create associations like in a relational database. One to One, One To Many, Many to Many.

    So you create a Model (like a table definition) with a primary key (idProperty) and relations. And the getters are automatically created.

    So within the record (instance of a model class) you can get a single record or get a store.

    This is where the answer to your question comes in. When you get the store it already has the filter set for getting the associated records. Say getting all notes... but you still have to execute the load method on this store. you can add more filters to the store prior to executing the load method. This would be like adding parameters. And from within the NOTE record you can get the customer record associated with that note.... that is the One (customer) to Many (notes) association.

    Code:
    Ext.define('Dcs.model.Note', {
        extend: 'Dcs.model.Base',
    	idProperty: 'noteId',
        fields: [
           { name: 'noteId', type: 'string'},
           { name: 'noteText', type: 'string' },
           { name: 'noteType', type: 'string' },
           { name: 'customerCode', type: 'string', reference: {
    				type: 'Customer',
    				role: 'customer',
    			}
    	   }
    }
    
    
    Ext.define('Dcs.model.Customer', {
        extend: 'Dcs.model.Base',
    	idProperty: 'customerCode',
        fields: [
           { name: 'customerCode', type: 'string' },
           { name: 'name', type: 'string' },
           { name: 'customerType', type: 'string'}
    }
    
    
    ,


    In this case you setup with the Model on the many side and the one side is automatically created.

    so if you had a Note record you could say:

    Code:
    let customer = note.getCustomer({
        callback: function() { console.log("got the customer"); }
    });
    this would be like running the load method on a customer object so there is a callback function.

    I think that Extjs is smart enough that if the customer with the given primary key is already in the schema it will come from there but I am not competently sure of that.

    And if you already had the customer you can get a store with all the notes:

    let noteStore = customer.notes();

    So you have a store that is setup with a filter to get the notes for that customer but you can add other filters.
    Code:
    noteStore.addFilter(
    new Ext.util.Filter({
        property: 'noteType',
        value: 'smallNote',
        operator: '='
    });
    
    noteStore.load():

    I have had to do some work to get this to work with composite keys. But it is doable.

    So once you have the records and stores you can make changes to the values and call save (record) or sync (stores) and it will fire off calls to the back end to make the changes to the database.

    There is also keyless associatings for non sql (json?) databases. I have not used that so I don't know much about it.

    Good Luck.

    -Mark

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •