2013-03-26 67 views
1

在我的应用程序中,我有一个列表和详细信息(表单)。我想要在单击列表项时将数据加载到Detail视图(将数据设置为表单的textfields)。对于列表和详细信息,我都从远程服务器获取数据。我正在关注MVC。加载数据以形成mvc

现在,当单击listItem时,我能够从服务器获取数据并将其保存到存储并显示详细视图。但我无法将数据从商店绑定到文本框中。

型号

Ext.define('App.model.Details', { 
    extend: 'Ext.data.Model', 
    config: { 
     fields: [ 
      {name: 'Name', type: 'string'}, 
      {name: 'BillingStreet', type: 'string'}, 
      {name: 'BillingCity', type: 'string'} 

     ] 
    } 
}); 

商店

Ext.define('App.store.Details', { 
    extend: 'Ext.data.Store', 
    config: { 
     model: 'App.model.Details', 
     autoLoad :true, 

     grouper : function(record) { 
      return record.get('Name')[0]; 
     }, 
    } 
}); 

列表视图

Ext.define('App.view.View', { 
    extend: 'Ext.List', 
    alias:'widget.contactlist', 
    fullscreen: true, 
    id: 'contactlist', 
    config:{ 
     disableSelection:true, 
     store:'Contacts', 
     itemTpl:'{Name}', 
     items:[ 
     { 
      xtype:'toolbar', 
      docked:'top', 
      title:'Leeds List' 
     } 

     ] 
    } 
}); 

详图

Ext.define("App.view.ListDetail", { 
    extend: "Ext.form.Panel", 
    requires: "Ext.form.FieldSet", 
    alias: "widget.listDetail", 
    config:{ 
     scrollable:'vertical' 
    }, 
    initialize: function() { 

     this.callParent(arguments); 



     var topToolbar = { 
      xtype: "toolbar", 
      docked: "top", 
      title: "Details" 

     }; 


     this.add([ 
        topToolbar, 
        { xtype: "fieldset", 
         items: [{ 
          xtype: 'textfield', 
          store: 'Details', 
          value : 'Name', 
          label: 'Name' 
         }, 
         { 
          xtype: 'emailfield', 
          store: 'Details', 
          value : 'BillingStreet', 
          label: 'Email' 
         }, 
         { 
          xtype: 'passwordfield', 
          store: 'Details', 
          value : 'BillingCity', 
          label: 'Password' 
         } 

         ] 
        } 
       ]); 


    } 

});

控制器

Ext.define('App.controller.Main', { 

    extend: 'Ext.app.Controller', 

     config: { 
     refs: { 
      // We're going to lookup our views by xtype. 
      contactlist: "contactlist", 
      contactdetail: "listDetail", 
      //f_name:"#f_name" 


     }, 
     control: { 
      contactlist: { 
       // The commands fired by the notes list container. 
       itemtap: "oneditLeadCommand" 

      } 

     }, 

     routes: { 
      'contactlist': 'activateList' 
     } 
    }, 

    activateList: function() 
    { 
     Ext.Viewport.animateActiveItem(this.getContactlist(), this.slideRightTransition); 
    }, 

    slideLeftTransition: { type: 'slide', direction: 'left' }, 
    slideRightTransition: { type: 'slide', direction: 'right' }, 

    oneditLeadCommand: function (list, index, target, record, e, eOpts) 
    { 

     console.log("onEditLead"+record.data.Id); 
     this.activateLeadDetail(record); 


    }, 

    activateLeadDetail: function (record) 
    { 

     var contactDetail = this.getContactdetail(); 
     //console.log("activateLeadDetail"+contactDetail.textfield); 
     //contactDetail.setRecord(record); // load() is deprecated. 
     //this.getF_name().setDisplayField(""); 


     store = Ext.StoreMgr.get('Details'); 
     //console.log("activateLeadDetail"+store); 
     store.setProxy({ 
     type: 'ajax', 
     url : 'http://10.0.2.2:8080/SalesForce/leads/get-lead-details/00D90000000jvoU!AR4AQB6Xcjz4UNBKf12WOcYHWc31QxK2.fXTcbCvOq.oBosCrjBezhqm8Nqc1hrf8MKK5LjLAu8ZC5IqB1kdpWvJGLdWd5pJ/'+record.data.Id, // the json file that holds all our contact info. 
     reader: { 
      type: 'json' 
      } 
     }); 
     store.load(); 
     var record1 = Ext.StoreMgr.get('Details').getAt(0); 
     console.log("activateLeadDetail"+record1); 

     Ext.StoreMgr.get('Details').each(function(test){ 
      console.log("for loop"+test.data); 
     }); 
     contactDetail.setRecord(record1); 
     Ext.Viewport.animateActiveItem(contactDetail, this.slideLeftTransition); 
    }, 
    // Base Class functions. 
    launch: function() { 
     this.callParent(arguments); 

     console.log("launch"); 
    }, 
    init: function() { 
     this.callParent(arguments); 
     console.log("init"); 
    } 
}) 

请帮忙将数据绑定到详细信息视图。

回答

4

所以我猜你的联系人商店是在其他地方定义的,但是因为这个工作正常,所以你没有在这里粘贴代码。

因此,在模型上的一个快速注释,你应该在总是定义一个idProperty。这是Sencha在内部用于定义商店“主键”的功能,因此在您重新加载/刷新商店时可正常使用。

Ext.define('App.model.Details', { 
    extend: 'Ext.data.Model', 
    config: { 
     idProperty: 'Name', // Or something from your server maybe? 
     fields: [ 
      {name: 'Name', type: 'string'}, 
      {name: 'BillingStreet', type: 'string'}, 
      {name: 'BillingCity', type: 'string'} 

     ] 
    } 
}); 

其次,你为什么要使用初始化方法在ListDetail视图当你在你的ListView使用的配置方法?如果指定的配置相反,你将能够通过执行类似

items: [ 
    { 
     xtype: 'ListDetail', 
     anyOtherAttribute: value 
    } 
] 

重用一些该组件的更简单的方法在其他地方,但还有点儿超出范围在这里。但不管怎么说。所以我认为这里有什么问题是您为面板的每个字段定义了一个商店。对不起,我不能测试我的假设,但这里是我会做什么:

Ext.define("App.view.ListDetail", { 
    extend: "Ext.form.Panel", 
    requires: "Ext.form.FieldSet", 
    alias: "widget.listDetail", 
    config:{ 
     scrollable:'vertical' 
     items:[ 
      { 
       xtype: "toolbar", 
       docked: "top", 
       title: "Details" 
      }, 
      { 
       xtype: "fieldset", 
       itemId: 'detailedListFiledset', //Overall, prefer itemId 
       items: [ 
        { 
         xtype: 'textfield', 
         value : 'Name', 
         label: 'Name' // may be you want placeholders here? 
        }, 
        { 
         xtype: 'emailfield', 
         value : 'BillingStreet', 
         label: 'Email' // and here.. 
        }, 
        { 
         xtype: 'passwordfield', 
         value : 'BillingCity', 
         label: 'Password' // and here.. 
        } 

        ] 
      } 
     ] 
    } 
}); 

好了,现在的问题似乎是在你的控制器:

  • 添加裁判您的字段集
  • 添加引用您的商店
  • 创建后负荷回调时,你的店被加载(详细信息)
  • 请清除店每次或追加数据,并适用于过滤,以获得正确的记录(这是为什么idProperty是非常有用的)
  • 设置字段集的记录和不是面板

我还没有尝试的机会,但我会做到这一点今晚稍后。但尽管如此,你还是可以试试。

- 编辑 -

好,我终于得到了安倍晋三为你代码的东西。

有几个问题出现在您的代码中。我不知道你为什么需要两家商店,但是让我们假设你是这样做的。我会给你我使用的所有文件(两个商店,两个模型,三个视图和控制器)。代码中存在三个主要错误:

  • 您不应加载第二个存储并尝试获取记录。使用和“加载”或“刷新”事件
  • 表单的SetValues是使用的正确函数
  • 您在表单中缺少名称属性,以便表单知道商店/模型的哪个值绑定到该字段。

ContactModel:

Ext.define('App.model.Contact', { 
    extend: 'Ext.data.Model', 
    config: { 
     idProperty: 'id', 
     fields: [ 
      {name: 'id', type: 'int'}, 
      {name: 'name', type: 'string'} 
     ] 
    } 
}); 

ContactStore的:

Ext.define('App.store.ContactStore', { 
    extend: 'Ext.data.Store', 
    requires:['App.model.Contact'], 
    config: { 
     storeId: 'ContactStore', 
     model: 'App.model.Contact', 
     autoLoad :true, 
     data: [ 
      {id: 0, name: 'Foo'}, 
      {id: 1, name: 'Bar'} 
     ] 
    } 
}); 

DetailModel:

Ext.define('App.model.Detail', { 
    extend: 'Ext.data.Model', 
    config: { 
     idProperty: 'id', 
     fields: [ 
      {name: 'id', type: 'int'}, 
      {name: 'name', type: 'string'}, 
      {name: 'billingStreet', type: 'string'}, 
      {name: 'billingCity', type: 'string'} 
     ] 
    } 
}); 

DetailStore:

Ext.define('App.store.DetailStore', { 
    extend: 'Ext.data.Store', 
    config: { 
     model: 'App.model.Detail', 
     autoLoad :true, 
     data: [ 
      {id: 0, name: 'Foo', billingStreet:'here', billingCity: 'Somewhere'}, 
      {id: 1, name: 'Bar', billingStreet:'there', billingCity: 'Somewhere else'} 
     ] 
    } 
}); 

ContactView:

Ext.define('App.view.ContactList', { 
    extend: 'Ext.List', 
    xtype: 'contactList', 
    fullscreen: true, 

    config: { 
     itemId: 'contactList', 
     store:'ContactStore', 
     emptyText: 'test', 
     itemTpl: new Ext.XTemplate(
      '{name}' 
     ), 
     items:[ 
      { 
       xtype:'toolbar', 
       docked:'top', 
       title:'Leeds List' 
      } 
     ] 
    } 
}); 

的DetailView:

Ext.define('App.view.Detail', { 
    extend: 'Ext.form.Panel', 
    requires: ['Ext.form.FieldSet'], 
    xtype: "detail", 
    config:{ 
     scrollable:'vertical', 
     items: [ 
      { 
      xtype: 'toolbar', 
      docked: 'top', 
      title: 'Details' 
      }, 
      { 
      xtype: 'fieldset', 
      itemId: 'detailForm', 
      items: [{ 
       xtype: 'textfield', 
       store: 'Details', 
       name: 'name', 
       placeHolder : 'Name', 
       label: 'Name' 
       }, 
       { 
       xtype: 'textfield', 
       store: 'Details', 
       placeHolder : 'BillingStreet', 
       name: 'billingStreet', 
       label: 'BillingStreet' 
       }, 
       { 
       xtype: 'textfield', 
       store: 'Details', 
       placeHolder : 'BillingCity', 
       name: 'billingCity', 
       label: 'BillingCity' 
       } 
      ] 
      } 
     ] 
    } 
}); 

主要观点:

Ext.define('App.view.Main', { 
    extend: 'Ext.Container', 
    xtype: 'main', 
    config: { 
     layout: 'hbox', 
     items: [ 
      { 
       xtype: 'contactList', 
       flex:1 
      }, 
      { 
       xtype: 'detail', 
       flex:2.5 
      } 
     ] 
    } 
}); 

主控制器:

Ext.define('App.controller.Main', { 
    extend : 'Ext.app.Controller', 
    requires: [ 
     'Ext.Toolbar', 
     'Ext.List', 
     'App.store.ContactStore', 
     'App.store.DetailStore', 
     'App.view.Detail', 
     'App.view.ContactList' 
    ], 
    config: { 
     //@private 
     detailStore: null, 
     currentListIndex: -1, 
     views : [ 
      'App.view.ContactList', 
      'App.view.Detail' 
     ], 
     refs: { 
      list: 'contactList', 
      detail: 'detail', 
      detailForm: 'detail #detailForm' 
     }, 
     control: { 
      list: { 
       itemtap: 'handleItemTapList' 
      } 
     } 
    }, 
    launch: function() { 
     var store = Ext.getStore('DetailStore'); 
     store.on('refresh', 'handleDetailStoreLoad', this); 
     this.setDetailStore(store); 
    }, 
    handleItemTapList: function(list, index, target, record) { 
     this.setCurrentListIndex(index); 
     this.getDetailStore().load(); 
    }, 
    handleDetailStoreLoad: function (store) { 
     debugger; 
     var record = store.getAt(this.getCurrentListIndex()); 
     this.getDetail().setValues(record.data); 
    } 

}); 

我们可以争论几件事情,但我试图直截了当地让它工作。如果你有更多的问题,请询问,但这个例子正在为我工​​作。在我看来,您可能不需要第二家商店,因为联系详情可以嵌套在COntact商店中,您可以使用商店的hasMany属性。

+0

嗨皮埃尔,我是Sencha新手,可以请用源代码解释我 – 2013-03-27 04:56:00

+0

请看到上面的编辑,如果这是好的! – Pierre 2013-03-27 21:33:30

+0

感谢你的时间和精力 – 2013-03-28 05:20:28

1

使用form.setRecord()来记录加载到形式

确保你的表单元素具有匹配的型号名称性能

因为你有一个商店,你将拥有该名称的属性值请使用getAt()或find()方法在商店中获取或找到一个模型

+0

他正在做它的控制器“contactDetail.setRecord(record1);”但自从他开始在商店之前的几行前调用.load(),并且因此商店删除所有数据之后,该商店中肯定没有记录。我最好的选择是record1是不确定的? – Pierre 2013-03-26 18:17:13

+0

在ExtJS 4.2.2中没有setRecord()方法,只是loadRecord()http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.form.Basic-method-loadRecord – 2014-06-18 12:31:58