ExtJS4 - 将多个模型添加到商店然后调用 store.sync 不会保存所有数据

ExtJS4 - Adding multiple model to a store then calling store.sync does not save all the data

美好的一天,我正在使用 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 个模型然后同步商店时,无论我向模型添加了多少项目,我都会得到一个空白行。

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

我不明白为什么你的代码没有按预期工作,但我看到了很大的改进空间:

  • Ext.ModelManager.create 弃用 使用 Ext.create('Your Model', data);
  • 应用程序名称和class名称以大写开头!
  • 避免在循环中声明变量(循环时日期不会改变...)
  • 在创建模型时声明日期(更快)否则使用 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);

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