【问题标题】:ExtJS4 - Adding multiple model to a store then calling store.sync does not save all the dataExtJS4 - 将多个模型添加到商店然后调用 store.sync 不会保存所有数据
【发布时间】:2015-01-19 09:40:46
【问题描述】:

美好的一天,我正在使用 ExtJS4 构建一个 Web 应用程序,我在其中创建了多个模型,然后将它们添加到商店,然后同步商店。但是,在调用store.sync() 时,我看到一个空白的数据库条目。

我的代码是这样的:

this.mon(uploadDialog, 'uploadcomplete', function(uploadPanel, manager, items, errorCount) {

    var itemCount = items.length;

    for(var i = 0 ; i < itemCount ; i++){
        pathArray.push(items[i].getName());
    }

    console.log('---------- pathArray count = ' + pathArray.length);

    var galleryStore = Ext.getStore('userProfileGallery');
    var userStore = Ext.getStore('userStore');
    var userModel = userStore.first();

    //------------ Iterate through the items, create a model, then add model to the store

    for(var j = 0 ; j < itemCount ; j++){
        var personPhotoModel = Ext.ModelManager.create({
        }, 'myappName.model.userPhoto');

        var currentdate = new Date();

        var uploadDate = currentdate.getDate() + "/" +
            (currentdate.getMonth()+1)  + "/" +
            currentdate.getFullYear();

        var uploadTime = currentdate.getHours() + ":" +
            currentdate.getMinutes() + ":" +
            currentdate.getSeconds();

        personPhotoModel.set("image_path", "gallery/" + pathArray[j]);
        personPhotoModel.set("description", "Gallery Image");
        personPhotoModel.set("upload_date", uploadDate);
        personPhotoModel.set("upload_time", uploadTime);
        personPhotoModel.set("user_id", userModel.get('user_id'));

        galleryStore.add(personPhotoModel);
        console.log('gallery store count = ' + galleryStore.count());

    }

    //---------- sync the store here

    console.log('gallery store count = ' + galleryStore.count());

    galleryStore.sync();

}, this);

到目前为止,我尝试通过仅上传一个文件来制作 1 个模型,并且效果很好。但是,当我向商店添加超过 1 个模型然后同步商店时,无论我向模型中添加了多少项目,我都会得到一个空白行。

非常感谢任何帮助。谢谢。

【问题讨论】:

    标签: extjs extjs4 extjs4.2


    【解决方案1】:

    我没有直接看到为什么您的代码无法按预期工作,但我看到了很大的改进空间:

    • Ext.ModelManager.create弃用使用Ext.create('Your Model', data);
    • 应用程序名和类名以大写开头!
    • 避免在循环中声明变量(循环时日期不会改变...)
    • 在创建模型时声明日期(更快)否则使用record.beginEdit()record.endEdit() 进行批量编辑。这可以防止每个set 触发事件

    这就是我的想法:

    this.mon(uploadDialog, 'uploadcomplete', function (uploadPanel, manager, items, errorCount) {
    
        var itemCount = items.length,
            now = new Date(),
            uploadDate = Ext.Date.format(now, 'd/m/Y'),
            uploadTime = Ext.Date.format(now, 'H:i:s'),
            i;
    
        for (i = 0; i < itemCount; i++) {
            pathArray.push(items[i].getName());
        }
    
        console.log('---------- pathArray count = ' + pathArray.length);
    
        var galleryStore = Ext.getStore('userProfileGallery'),
            userStore = Ext.getStore('userStore'),
            user = userStore.first();
    
        //------------ Iterate through the items, create a model, then add model to the store
    
        for (i = 0; i < itemCount; i++) {
    
            galleryStore.add(Ext.create('MyappName.model.UserPhoto', {
                image_path: "gallery/" + pathArray[i],
                description: "Gallery Image",
                upload_date: uploadDate,
                upload_time: uploadTime,
                user_id: user.get('user_id')
            }));
    
            console.log('gallery store count = ' + galleryStore.count());
        }
    
        //---------- sync the store here
    
        console.log('gallery store count = ' + galleryStore.count());
    
        galleryStore.sync();
    
    }, this);
    

    如果这不起作用,您的错误不在这段代码中,而是在商店、模型或后端

    【讨论】:

      猜你喜欢
      • 2012-08-13
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      相关资源
      最近更新 更多