Results 1 to 7 of 7

Thread: Add Formulas to ViewModel dynamically

  1. #1
    Sencha Premium Member ebett's Avatar
    Join Date
    Nov 2010
    Location
    Argentina
    Posts
    31
    Answers
    1

    Lightbulb Answered: Add Formulas to ViewModel dynamically

    Can I add formulas dynamically to the view model?

  2. Thank you, I'll try it. But, what if formulas is undefined?, Can I create json object and then setFormulas?
    Yes,
    Code:
    formulas = viewModel.getFormulas() || {};

  3. #2
    Sencha - Support Team
    Join Date
    Mar 2015
    Location
    Chicago
    Posts
    239
    Answers
    40

    Default

    Yes, you can use the viewModel.setFormulas() method to set the formulas manually, however, this method replaces the all formulas available, so if you already have some formulas added to your viewModel you should use the getFormulas() method and add the new formula to the object retrieved and then use the setFormulas().


    Here is a Fiddle to demonstrate this:

    Guilherme Lopes
    Sencha Support

  4. #3
    Sencha Premium Member ebett's Avatar
    Join Date
    Nov 2010
    Location
    Argentina
    Posts
    31
    Answers
    1

    Default

    Thank you, I'll try it. But, what if formulas is undefined?, Can I create json object and then setFormulas?
    Yes,
    Code:
    formulas = viewModel.getFormulas() || {};

  5. #4
    Sencha Premium User
    Join Date
    Jan 2014
    Location
    Fort Worth, TX, USA
    Posts
    64
    Answers
    2

    Default

    Quote Originally Posted by ebett View Post
    Code:
    formulas = viewModel.getFormulas() || {};
    That seems to work. I'm just adding a quick way to do this to the viewmodel config:

    Code:
        addFormulas: function (newForumlas) {
            var me = this,
                oldFormulas = me.getFormulas() || {},
                allFormulas;
    
    
            allFormulas = Ext.apply(oldFormulas, newForumlas);
    
    
            me.setFormulas(allFormulas);
        }

  6. #5
    Sencha Premium Member valio's Avatar
    Join Date
    Jan 2014
    Posts
    203
    Answers
    8

    Default

    It took me some time to realize this, but
    there is actually no need at all to use the getFormulas method!

    It is sufficent to call viewmodel.setFormulas() with some arbitrary JS object to extend the viewmodel with formulas (while keeping those that whose names are not defined as keys on the object).

    Let's say we have a viewmodel VM which is declared with the formulas {A:{...}, B:{...}}.
    Now we call VM.setFormulas({B:{...},C:{...}).
    This will keep formula A, overwrite formula B and add formula C.


    Also see this slightly adapted Fiddle

  7. #6
    Sencha Premium Member valio's Avatar
    Join Date
    Jan 2014
    Posts
    203
    Answers
    8

    Default

    I posted a bugreport for the docs.
    On a second thought the problem is probably not only the docs, but the method naming. Probably if would have been more intuitive if the method setFormulas was named applyFormulas (however that could again be confusing, as someone might assume that this means executing a manual formula recalculation).

  8. #7
    Sencha Premium User VDP's Avatar
    Join Date
    Feb 2012
    Location
    Niel, Belgium
    Posts
    101
    Answers
    4

    Lightbulb Adding formulas

    Quote Originally Posted by davmillar View Post
    That seems to work. I'm just adding a quick way to do this to the viewmodel config:

    Code:
        addFormulas: function (newForumlas) {
            var me = this,
                oldFormulas = me.getFormulas() || {},
                allFormulas;
    
    
            allFormulas = Ext.apply(oldFormulas, newForumlas);
    
    
            me.setFormulas(allFormulas);
        }

    That does not work correctly for me!

    say you setup a binding on a component to formula A, then you add a formulla B => the first formula A would be overridden and thus not update my component anymore until I fix that bind.

    So indeed the better option is to just 'setFormulas' that will just add the new formula an leaves the old formula or overrides if you add it with the same name. I completely agree that it should be named 'applyFormulas' or 'addFormulas' not 'setFormulas'

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
  •