Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未在child.eval上定义id_Javascript_Backbone.js - Fatal编程技术网

Javascript 未在child.eval上定义id

Javascript 未在child.eval上定义id,javascript,backbone.js,Javascript,Backbone.js,我正在编写我的第一个主干博客应用程序,但当我尝试添加新帖子时,它抛出了一个错误 这是我的app.js(所有与主干网相关的组件都在此文件中): 错误在哪里? 首先,您需要找到导致错误的原因,以及错误的来源 错误来自PostListView的render函数中的以下行: this.el.innerHTML = this.template(this.model.toJSON()); 下划线的模板呈现会引发错误。这归结为: template: _.template("<a href='/post

我正在编写我的第一个主干博客应用程序,但当我尝试添加新帖子时,它抛出了一个错误

这是我的app.js(所有与主干网相关的组件都在此文件中):

错误在哪里? 首先,您需要找到导致错误的原因,以及错误的来源

错误来自
PostListView
render
函数中的以下行:

this.el.innerHTML = this.template(this.model.toJSON());
下划线的模板呈现会引发错误。这归结为:

template: _.template("<a href='/posts/{{id}}'>{{title}}</a>"),
如何在集合的
create
调用后等待? 主干网为大多数(如果不是全部)异步函数提供了
success
error
回调

选项散列接受
success
error
回调,这两个回调都将是 作为参数传递
(集合、响应、选项)

因此,您可以将
createPost
函数更改为以下内容,添加一个
onPostCreated
回调

createPost: function(e) {
    e.preventDefault();
    var postAttrs = {
        content: $("#postText").val(),
        title: $("#postTitle").val(),
        pubDate: new Date(),
    };
    this.posts.create(postAttrs, {
        context: this,
        success: this.onPostCreated
    });
    return false;
},
onPostCreated: function() {
    // you don't need to use the router, so your views are self-contained
    Backbone.history.navigate("/", { trigger: true });
},
错误在哪里? 首先,您需要找到导致错误的原因,以及错误的来源

错误来自
PostListView
render
函数中的以下行:

this.el.innerHTML = this.template(this.model.toJSON());
下划线的模板呈现会引发错误。这归结为:

template: _.template("<a href='/posts/{{id}}'>{{title}}</a>"),
如何在集合的
create
调用后等待? 主干网为大多数(如果不是全部)异步函数提供了
success
error
回调

选项散列接受
success
error
回调,这两个回调都将是 作为参数传递
(集合、响应、选项)

因此,您可以将
createPost
函数更改为以下内容,添加一个
onPostCreated
回调

createPost: function(e) {
    e.preventDefault();
    var postAttrs = {
        content: $("#postText").val(),
        title: $("#postTitle").val(),
        pubDate: new Date(),
    };
    this.posts.create(postAttrs, {
        context: this,
        success: this.onPostCreated
    });
    return false;
},
onPostCreated: function() {
    // you don't need to use the router, so your views are self-contained
    Backbone.history.navigate("/", { trigger: true });
},
这可能会有帮助。这可能会有帮助。
this.posts.create(postAttrs);
// the model hasn't received an id from the server yet.
postRouter.navigate("/", { trigger: true });
createPost: function(e) {
    e.preventDefault();
    var postAttrs = {
        content: $("#postText").val(),
        title: $("#postTitle").val(),
        pubDate: new Date(),
    };
    this.posts.create(postAttrs, {
        context: this,
        success: this.onPostCreated
    });
    return false;
},
onPostCreated: function() {
    // you don't need to use the router, so your views are self-contained
    Backbone.history.navigate("/", { trigger: true });
},