0

Ok guys, I originally set out to render the users' NewView onto my root page. I have gotten to the point where I can see the form, but when I fill it out and click "Create User" I get this error:

TypeError: this.model is undefined

$(this.el).html(this.template(this.model.toJSON()));

Here is the relevant code:

index.html.erb

<div id="container">Loading...</div>

<script type="text/javascript">
  $(function() {
    window.newView = new Example.Views.Users.NewView({model: <%= @users.to_json.html_safe -%>,collection: new Example.Collections.UsersCollection()});
    window.router = new Example.Routers.UsersRouter({users: <%= @users.to_json.html_safe -%>});
    $('body').html(newView.render().$el);
    Backbone.history.start();
  });
</script>

Here is one question I posted today which has brought me to this question...reading the comments should be helpful!

ReferenceError: users is not defined rails + backbone + firebug

I have a feeling it has something to do with the fact that I created two windows? "window.newView" and "window.router".

There needs to be a router for Backbone.history.start() to work...correct? This is why I added the router line in the first place...because I was getting an error with Backbone.history.start().

4

1 回答 1

0

我认为这可行,我将“pushState:true”传递给启动方法...至少不会引发任何错误...我可以通过并注册、显示和销毁用户...它不会重定向但是,在索引页面上创建用户时,一旦创建用户,就会显示页面。

<div id="container">Loading...</div>

<script type="text/javascript">
  $(function() {
    window.newView = new Example.Views.Users.NewView({model: <%= @users.to_json.html_safe -%>,collection: new Example.Collections.UsersCollection()});
    window.router = new Example.Routers.UsersRouter({users: <%= @users.to_json.html_safe -%>});
    $('body').html(newView.render().$el);
    Backbone.history.start({pushState: true});
  });
</script>

如果我的修复有任何问题,请纠正我。

于 2013-05-23T19:47:04.467 回答