Results 1 to 9 of 9

Thread: MVVM: Binding record field value as an expression to component property

  1. #1
    Sencha User
    Join Date
    Mar 2011
    Location
    Arvada, CO
    Posts
    404
    Answers
    11

    Default Answered: MVVM: Binding record field value as an expression to component property

    I'm new to Ext JS 5 (using v5.1), and I'm still trying to figure out how to do things the MVVM way. So let's say I've got a ViewController, ViewModel, and my View. In the View, I've got a form panel that gets a loaded record. When this record loads into the form, I want to hide or show a button based on the record's "status" field, so I figured do something with binding... but it looks like binding is limited to only inverting, not actually using an expression. To get a better understanding, take a look at this code:

    Code:
    Ext.application({
        name : 'Fiddle',
    
        launch : function() {
            Ext.define('User', {
               extend: 'Ext.data.Model',
               fields: ['name', 'status']    
            });
            
            Ext.define('UserListController', {
                extend : 'Ext.app.ViewController',
                alias: 'controller.userlist'
            });
            
            Ext.define('UserListViewModel', {
                extend: 'Ext.app.ViewModel',
                alias: 'viewmodel.userlist'
            });
            
            Ext.define('UserList', {
                extend: 'Ext.form.Panel',
                controller: 'userlist',
                viewModel: 'userlist',
            
                tbar: [{
                    text: 'Add',
                    reference: 'addButton',
                    bind: {
                        //hidden: '{status == 2}'
                    }
                }, {
                    text: 'Delete',
                    reference: 'deleteButton',
                    bind: {
                        //hidden: '{status == 1}'
                    }
                }],
                items: [{
                    xtype: 'displayfield',
                    name: 'name',
                    fieldLabel: 'Name'
                }, {
                    xtype: 'displayfield',
                    name: 'status',
                    fieldLabel: 'Status'
                }]
            });
            
            var myForm = Ext.create('UserList', {
                width: 400,
                height: 200,
                renderTo: Ext.getBody()
            });
            
            var record = Ext.create('User', {
                name: 'blah',
                status: 2
            });
            
            myForm.loadRecord(record);
            if (record.get('status') === 2) {
                myForm.lookupReference('addButton').hide();
            }
        }
    });
    As you can see, I'm currently just probing the values of the record to hide the addButton. Is there anyway I can accomplish this with binding? It's good to note that I also looked at formulas, but from what I understanding, that's only for changing how data is rendered, so it didn't seem like the proper route.

  2. First of all, I wouldn't load record to form, I would directly bind form fields to record fields. It's much less work and it is very straightforward. Then, I'd create a formula that calculates whether the button should be shown or hidden and then I'd bind hidden property to the formula. You can see almost identical approach here: http://extjs.eu/ext-examples/#vm-hierarchy - see mainly the form view configuration and its view model.

  3. #2
    Sencha - Community Support Team jsakalos's Avatar
    Join Date
    Apr 2007
    Location
    Slovakia
    Posts
    27,664
    Answers
    512

    Default

    First of all, I wouldn't load record to form, I would directly bind form fields to record fields. It's much less work and it is very straightforward. Then, I'd create a formula that calculates whether the button should be shown or hidden and then I'd bind hidden property to the formula. You can see almost identical approach here: http://extjs.eu/ext-examples/#vm-hierarchy - see mainly the form view configuration and its view model.
    Jozef Sakalos, aka Saki

    Education, extensions and services for developers at new http://extjs.eu
    News: Grid MultiSearch Plugin, Grid MultiSort Plugin, Configuring ViewModel Hierarchy


  4. #3
    Sencha User
    Join Date
    Mar 2011
    Location
    Arvada, CO
    Posts
    404
    Answers
    11

    Default

    Quote Originally Posted by jsakalos View Post
    First of all, I wouldn't load record to form, I would directly bind form fields to record fields. It's much less work and it is very straightforward. Then, I'd create a formula that calculates whether the button should be shown or hidden and then I'd bind hidden property to the formula. You can see almost identical approach here: http://extjs.eu/ext-examples/#vm-hierarchy - see mainly the form view configuration and its view model.
    I had a big response all typed out, but I think in doing that, I realized I should probably be leveraging data binding more in my application. Either way, I'm curious to know how you would submit this form data if it's not actually tied to a model... as I can't do form.getRecord. Would you call a store.sync?

  5. #4
    Sencha - Community Support Team jsakalos's Avatar
    Join Date
    Apr 2007
    Location
    Slovakia
    Posts
    27,664
    Answers
    512

    Default

    Yes, if the record is in a store then store.sync(). If not then model.save().
    Jozef Sakalos, aka Saki

    Education, extensions and services for developers at new http://extjs.eu
    News: Grid MultiSearch Plugin, Grid MultiSort Plugin, Configuring ViewModel Hierarchy


  6. #5
    Sencha User
    Join Date
    Mar 2011
    Location
    Arvada, CO
    Posts
    404
    Answers
    11

    Default

    Quote Originally Posted by jsakalos View Post
    Yes, if the record is in a store then store.sync(). If not then model.save().
    Interesting... I'm just so used to doing a plain old Ajax request because I feel like I have more control over that, but I guess a model.save is the same... Ext just knows whether to do a PUT/POST based on the data? I'll have to experiment with that.

  7. #6
    Sencha - Community Support Team jsakalos's Avatar
    Join Date
    Apr 2007
    Location
    Slovakia
    Posts
    27,664
    Answers
    512

    Default

    You can configure if you want POST or GET or you can use Rest proxy that uses PUT for updates and POST for creates. See the docs, there's no need to go down to the raw ajax request unless you do something very, very special.
    Jozef Sakalos, aka Saki

    Education, extensions and services for developers at new http://extjs.eu
    News: Grid MultiSearch Plugin, Grid MultiSort Plugin, Configuring ViewModel Hierarchy


  8. #7
    Sencha User
    Join Date
    Mar 2011
    Location
    Arvada, CO
    Posts
    404
    Answers
    11

    Default

    Quote Originally Posted by jsakalos View Post
    You can configure if you want POST or GET or you can use Rest proxy that uses PUT for updates and POST for creates. See the docs, there's no need to go down to the raw ajax request unless you do something very, very special.
    Yeah, I'm thinking you're right... I started using the whole model.save approach, and it's pretty wonderful. Just hope I don't run into any pitfalls. Thanks Saki! Always a pleasure chatting with you.

  9. #8
    Sencha - Community Support Team jsakalos's Avatar
    Join Date
    Apr 2007
    Location
    Slovakia
    Posts
    27,664
    Answers
    512

    Default

    Thanks.

    You SHALL run into problems for sure. But it is what's life all about, isn't it?
    Jozef Sakalos, aka Saki

    Education, extensions and services for developers at new http://extjs.eu
    News: Grid MultiSearch Plugin, Grid MultiSort Plugin, Configuring ViewModel Hierarchy


  10. #9
    Sencha User
    Join Date
    Mar 2011
    Location
    Arvada, CO
    Posts
    404
    Answers
    11

    Default

    Haha, I hear that, brother.

Similar Threads

  1. MVVM, Remote store and ViewModel binding to GridPanel
    By John Sourcer in forum Ext 5: Q&A
    Replies: 7
    Last Post: 2 Apr 2015, 7:49 PM
  2. Data binding and associated objects via MVVM
    By Jeremy Pugh in forum Ext 5: Q&A
    Replies: 1
    Last Post: 7 Jan 2015, 10:15 AM
  3. Dynamic store one way binding in MVVM
    By venishkhant in forum Ext 5: Q&A
    Replies: 1
    Last Post: 23 Sep 2014, 5:40 AM
  4. MVVM Binding Issue
    By Bramaram Naik Nenavath in forum Ext 5: Q&A
    Replies: 3
    Last Post: 30 Jul 2014, 8:36 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
  •