在呈现子视图之前等待集合数据



我正在构建一个应该相当简单的项目,该项目在很大程度上基于 Ampersand 的入门项目(当您第一次运行 ampersand 时(。"我的添加"页有一个 <select> 元素,应使用其他集合中的数据填充该元素。我一直在将此视图与编辑页面视图进行比较,因为我认为它们非常相似,但我无法弄清楚。

表单子视图有一个waitFor属性,但我不知道它期望什么类型的值 - 我知道它应该是一个字符串 - 但该字符串代表什么?

下面您可以看到我正在尝试获取app.brandCollection并将其值设置为 this.model这是正确的吗?我需要修改输出并将数据传递到具有正确格式的 & 符号选择视图元素;这是我的下一个问题。如果有人对此有建议,我将不胜感激。

var PageView = require('./base');
var templates = require('../templates');
var ProjectForm = require('../forms/addProjectForm');

module.exports = PageView.extend({
    pageTitle: 'add project',
    template: templates.pages.projectAdd,
    initialize: function () {
        var self = this;
        app.brandCollection.fetch({
            success : function(collection, resp) {
                console.log('SUCCESS: resp', resp);
                self.brands = resp;
            },
            error: function(collection, resp) {
                console.log('ERROR: resp', resp, options);
            }
        });
    },
    subviews: {
        form: {
            container: 'form',
            waitFor: 'brands',
            prepareView: function (el) {    
                return new ProjectForm({
                    el: el,
                    submitCallback: function (data) {
                        app.projectCollection.create(data, {
                            wait: true,
                            success: function () {
                                app.navigate('/');
                                app.projectCollection.fetch();
                            }
                        });
                    }
                });
            }
        }
    }
});

这只是添加页面视图,但我认为这就是所需要的。

表单子视图有一个 waitFor 属性,但我不知道它期望什么类型的值 - 我知道它应该是一个字符串 - 但该字符串代表什么?

此字符串表示具有固定this上下文的当前对象中的路径。在您的示例中,您已经waitFor: 'brands'这里只PageView.brands,因为PageView this上下文。如果你有 model.some.attribute ,那么这意味着这个字符串表示PageView.model.some.attribute 。这只是遍历对象的便捷方式。

很少

有信息可以回答你的后一个问题。您以什么形式检索数据?你以后想用它做什么?如果您能 https://gitter.im/AmpersandJS/AmpersandJS :) ping 我们,那就快多了

最新更新