Results 1 to 4 of 4

Thread: How to make changes to an association store trigger binds on Model?

  1. #1
    Sencha Premium Member jsamhall's Avatar
    Join Date
    May 2012
    Posts
    21
    Answers
    1
    Vote Rating
    0
      0  

    Default How to make changes to an association store trigger binds on Model?

    Hello all,

    I have a typical Order hasMany Items data model. The Items are of course represented by a Grid. The Grid is contained within a Window which I call the Form Window. It holds the "Save" button.

    When booting the Grid, I assign the Items association store (from the Order, i.e., order.getItems()) to the Grid's ViewModel. This works great!

    I have a formula in the Window's ViewModel that controls the enabled/disabled state of the "Save" button based on the validity of the Order. That formula is what you might expect:

    Code:
            enableSaveButton: {
                bind: {
                    bindTo: '{orderEntity}', 
                    deep  : true
                },
    
    
                get: function (order) {
                    return order 
                         && order.isValid() 
                         && this.get('items').count(); 
                }
            }
    The issue is that when Records are added to the VM Store (and therefore, the association store on the Order) it does not trigger the bind and therefore the "Save" button stays disabled until a change to a property on the Order entity triggers the bind (e.g., user changes the Order Number)

    Is there something I'm missing here, or is it intentional functionality that changes to an Association Store do not trigger binds to the owning Entity to fire?

    Of course I do something like, listen to dataChanged event on the Store and set some arbitrary property on the Entity (e.g., order.set('items_count', items.count()) in order to get the Bind to fire but I don't think this should be necessary

    Any ideas or guidance would be great, thank you!

  2. #2
    Sencha Premium Member jsamhall's Avatar
    Join Date
    May 2012
    Posts
    21
    Answers
    1
    Vote Rating
    0
      0  

    Default

    I ended up doing things a little differently.

    First, I assign the Association Store to a data property on the VM rather than as a Store on the VM. Functionally there is no difference, but VM.get('items') must be used instead of VM.getStore('items'). No big deal there.

    I've added a method to my base ViewModel that I found here: https://www.sencha.com/forum/showthr...-its-listeners. This allows for a formula to be manually invoked.

    I've added a datachanged listener to the Store which tells the "enableSaveButton" formula to run itself.

    I still feel as though this is a bandaid and I think it would be nice and make perfect sense if changes to associations triggered bindings to the owning entity to fire. I'd love to hear a dev's input on this!

  3. #3
    Sencha Premium Member jsamhall's Avatar
    Join Date
    May 2012
    Posts
    21
    Answers
    1
    Vote Rating
    0
      0  

    Default

    I failed to mention that I was previously running on 6.0.2

    After upgrading to 6.5.2, I can now bind directly to the association store, which solved part of my problem.

    For example:
    Code:
            xtype    : 'order-items-grid',
            reference: 'itemsGrid',
            flex     : 1,
            bind     : {
                store: '{order.lines}'
            }
    I still however, would like to understand how I might be able to "bubble" changes in an association store up to the Owner so that formulas bound to said Owner will trigger as this remains desirable functionality for me.

    For example, my Order entity has both a Billing and Shipping address. These are identified by a "type_code" i.e., "billing" or "shipping". This is handled well by Doctrine2's polymorphic discriminator:
    Code:
     * @ORM\DiscriminatorMap({
     *     "shipping"   = "ShippingAddress",
     *     "billing"   = "BillingAddress",
     * })
    In the Ext side of things, both addresses exist within the same Store and are managed via getter/setter on the Order model:
    Code:
        setShippingAddress: function (address) {
            // ... code that manages removing an address of the same type from the association store 
            this.addresses().add(address);
        },
    I then have a Formula in the VM for each Address field to get/set from.

    VM Formula
    Code:
            shippingAddress: {
                bind: '{orderEntity}',
                get : function (orderEntity) {
                    return orderEntity.getShippingAddress();
                },
                set: function (address) {
                    this.get('orderEntity').setShippingAddress(address);
                }
            }
    View configuration
    Code:
                        xtype     : 'address-picker',
                        reference : 'shipToAddressPicker',
                        fieldLabel: 'Ship To',
                        bind      : {
                            address: '{shippingAddress}'
                        }
    All that works splendidly, but because the Address is managed via an Association store, it does not cause other Formulas bound to the Order to trigger

    Code:
            enableSaveButton: {
                bind: {
                    bindTo: '{orderEntity}',
                    deep  : true
                },
    
                // this will not be run when changes to order.addresses() occur :(
                get: function (orderEntity) {
                    return orderEntity.isValid();
                }
            }
    Am I doing this all wrong or is this just something that doesn't work this way?

  4. #4
    Sencha Premium Member
    Join Date
    Dec 2011
    Posts
    43
    Vote Rating
    1
      0  

    Default

    im currently facing something similar... I have a Model containing various level of associations, mainly 'hasMany', and Im struggling on bindings/listening "deeply" on any changes made to these associations.

Similar Threads

  1. Store model and Association with a JSON Map
    By plgagnon in forum Ext: Q&A
    Replies: 2
    Last Post: 20 Aug 2016, 6:02 PM
  2. Why is an Association applied to a Model and not a Store?
    By DonRul in forum Sencha Architect 2.x: Help & Discussions
    Replies: 2
    Last Post: 3 May 2012, 2:46 PM
  3. Model, Store, BelongsTo Association, xTemplate Example
    By preyz in forum Sencha Touch 1.x: Discussion
    Replies: 4
    Last Post: 25 Apr 2011, 8:14 AM

Tags for this Thread

Posting Permissions

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