【问题标题】:Why is my view in backbone not rendering?为什么我在主干中的视图没有呈现?
【发布时间】:2013-07-11 01:52:12
【问题描述】:

我正在尝试从我的主干视图呈现此模板,但它不起作用?任何人都知道我在这里做错了什么谢谢!我将包含用于视图的玉文件和用于主干 js 脚本的 main.js 文件。

玉文件

extends layout
block content   
    div.centerContent
        script(type="text/javascript", src="/js/main.js")

        h4 User goes here with equal before it no space
            div#user
                p #{firstName} #{lastName}
                p #{email}
                p #{phone}
                p #{birthday}
                button.edit Edit



        script(id="userTemplate", type ="text/template")
                p #{firstName} #{lastName}
                p #{email}
                p #{phone}
                p #{birthday}
                button.edit Edit

        script(id="userEditTemplate", type ="text/template")
            div
                form(action="#")
                    input(type="text", class="firstName", value=#{firstName}) input(type="text", class="lastName", value=#{lastName})
                    input(type="email", class="email", value=#{email})
                    input(type="number", class="phone", value=#{phone})
                    input(type="date", class="birthday", value=#{birthday})
                button.save Save
                button.cancel Cancel
        hr

Main.Js 文件

(function () {
    window.App = {
        Models: {},
        Collections: {},
        Views: {},
        Templates: {},
        Router: {}

    };

    // MODEL
    App.Models.User = Backbone.Model.extend({
        defaults: {
            firstName: 'first',
            lastName: 'last',
            email: 'Email',
            phone: '222',
            birthday: 'date'
        },

        validate: function (attrs) {
            if (!attrs.firstName) {
                return 'You must enter a real first name.';
            }
            if (!attrs.lastName) {
                return 'You must enter a real last name.';
            }
            if (attrs.email.length < 5) {
                return 'You must enter a real email.';
            }
            if (attrs.phone.length < 10 && attrs.phone === int) {
                return 'You must enter a real phone number, if you did please remove the dash and spaces.';
            }
            if (attrs.city.length < 2) {
                return 'You must enter a real city.';
            }
        },

        initialize: function() {
             user.on('invalid', function (model, invalid) {
                console.log(invalid);
            });
        }

    });



    //VIEW
    App.Views.User = Backbone.View.extend({
        model: App.Models.User,
        //tagName: 'div',
        //id: 'user',
        //className: 'userProfile',
        template: _.template($("#userTemplate").html());
        editTemplate: _.template($("#userEditTemplate").html());


        initialize: function (){

        }

        render: function() {
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        },

        events: {
            'click button.edit': 'editProfile',
        //  'click button.save': 'saveEdits',
            'click button.cancel': 'cancelEdits'
        },

        editProfile: function () {
            this.$el.html(this.editTemplate(this.model.toJSON()));

        },

cancelEdits: function() {
            this.render();
        }

    });
    //start history service
    Backbone.history.start();

    var user = new App.Views.User({el: 'div #user'});
    user.render();
})(); 

【问题讨论】:

  • 检查您的选择器:'div #user' 还是 'div#user'?
  • div#user 是你在玉中的称呼...还是我做错了什么
  • 选择器 'div #user' 表示 id="user"&lt;div&gt; 内,&lt;div id="user"&gt; 将是 div#user 或只是 #user 很好,因为 id 属性必须是唯一的。

标签: javascript node.js backbone.js pug


【解决方案1】:

我发现您没有为骨干视图分配模型。

所以你可以试试这个:

      var MyModel = Backbone.Model.extend({
        defaults: {
            name: 'first',
            lastName: 'last',
            email: 'Email',
            phone: '222',
            birthday: 'date'
        }
    });

    var MyView = Backbone.View.extend({
        el: "#user",
        model: MyModel,
        template: _.template($("#test-template").html()),
        render: function(){
            this.$el.html(this.template(this.model.toJSON()));
            return this;
        }
    });

    var myView = new MyView({model:new MyModel()});
    myView.render();

而测试模板是:

<script id="test-template" type="text/template">
   <div><%=name%></div>
</script>

HTML:

<body>
<div id="user">
</div>
</body>

您可以在 Backbone 视图声明中定义主干视图的 el 属性。并记住在启动时为视图分配模型。

[编辑]

玉:

script(id="userTemplate", type ="text/template")
            p #{firstName} #{lastName}
            p #{email}
            p #{phone}
            p #{birthday}
            button.edit Edit

修改模板生成方式:

<script src="https://raw.github.com/weepy/jade-browser/master/jade.js"></script>
<script src="https://raw.github.com/weepy/jade-browser/master/jade-shim.js"></script>

template: jade.compile($("#userTemplate").text());

希望这对你有帮助。

【讨论】:

  • 我尝试在新视图中添加模型并将 el 移动到视图内部,但它仍然不适合我。
  • 好调用,得到这个未捕获的类型错误:无法调用未定义的 underscore.min.js:1 w.template underscore.min.js:1(匿名函数)main.js:56 的方法“替换”模板:_.template($("#userTemplate").html()), (匿名函数) main.js 112 })();
  • 您的模板声明似乎有问题。
  • 我也看到了,但不确定是什么问题,我尝试在窗口外评论它。应用模板{}但错误仍然存​​在...
  • 我原来是这样做的,在 Jade 中不起作用,它必须是 #{name}
猜你喜欢
  • 1970-01-01
  • 2015-01-31
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-08
相关资源
最近更新 更多