与backbone.js的简单rails集成
使用rails和主干呈现第一页时遇到问题。请假设我已经正确安装并运行了所有gems。大多数coffeescript文件都是使用“rails g主干:scaffold Student”生成的,但我更改了一些文件和目录结构。这只是试图理解流程,而不是其他任何东西。我还在数据库中加载了一些学生数据。最后,当部署(开发)这个rails应用程序并访问localhost:3000时,我没有收到initialize函数中的警报 这是文件与backbone.js的简单rails集成,backbone.js,ruby-on-rails-3.2,Backbone.js,Ruby On Rails 3.2,使用rails和主干呈现第一页时遇到问题。请假设我已经正确安装并运行了所有gems。大多数coffeescript文件都是使用“rails g主干:scaffold Student”生成的,但我更改了一些文件和目录结构。这只是试图理解流程,而不是其他任何东西。我还在数据库中加载了一些学生数据。最后,当部署(开发)这个rails应用程序并访问localhost:3000时,我没有收到initialize函数中的警报 这是文件 app/models/students.rb class Studen
app/models/students.rb
class Student < ActiveRecord::Base
attr_accessible :dob, :email, :name, :phone_number
end
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>School</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<tbody>
<div id="students">
<%= yield %>
</div>
</tbody>
</body>
</html>
app/assets/javascripts/school.js.coffee
#= require_self
#= require_tree ../templates
#= require_tree ./models
#= require_tree ./collections
#= require_tree ./views
#= require_tree ./routers
window.School =
Models: {}
Collections: {}
Routers: {}
Views: {}
initialize: (data) -> alert 'Hello for rails'
class School.Collections.StudentsCollection extends Backbone.Collection
model: School.Models.Student
url: '/students'
class School.Models.Student extends Backbone.Model
paramRoot: 'student'
class School.Routers.StudentsRouter extends Backbone.Router
initialize: (options) ->
@students = new School.Collections.StudentsCollection()
@students.reset options.students
routes:
"index" : "index"
".*" : "index"
index: ->
@view = new School.Views.Students.IndexView(students: @students)
$("#students").html(@view.render().el)
School.Views.Students ||= {}
class School.Views.Students.IndexView extends Backbone.View
template: JST["templates/students/index"]
initialize: () ->
@options.students.bind('reset', @addAll)
addAll: () =>
@options.students.each(@addOne)
addOne: (student) =>
view = new School.Views.Students.StudentView({model : student})
@$("tbody").append(view.render().el)
render: =>
@$el.html(@template(students: @options.students.toJSON() ))
@addAll()
return this
app/assets/javascripts/collections/students.js.coffee
#= require_self
#= require_tree ../templates
#= require_tree ./models
#= require_tree ./collections
#= require_tree ./views
#= require_tree ./routers
window.School =
Models: {}
Collections: {}
Routers: {}
Views: {}
initialize: (data) -> alert 'Hello for rails'
class School.Collections.StudentsCollection extends Backbone.Collection
model: School.Models.Student
url: '/students'
class School.Models.Student extends Backbone.Model
paramRoot: 'student'
class School.Routers.StudentsRouter extends Backbone.Router
initialize: (options) ->
@students = new School.Collections.StudentsCollection()
@students.reset options.students
routes:
"index" : "index"
".*" : "index"
index: ->
@view = new School.Views.Students.IndexView(students: @students)
$("#students").html(@view.render().el)
School.Views.Students ||= {}
class School.Views.Students.IndexView extends Backbone.View
template: JST["templates/students/index"]
initialize: () ->
@options.students.bind('reset', @addAll)
addAll: () =>
@options.students.each(@addOne)
addOne: (student) =>
view = new School.Views.Students.StudentView({model : student})
@$("tbody").append(view.render().el)
render: =>
@$el.html(@template(students: @options.students.toJSON() ))
@addAll()
return this
app/assets/javascripts/models/student.js.coffee
#= require_self
#= require_tree ../templates
#= require_tree ./models
#= require_tree ./collections
#= require_tree ./views
#= require_tree ./routers
window.School =
Models: {}
Collections: {}
Routers: {}
Views: {}
initialize: (data) -> alert 'Hello for rails'
class School.Collections.StudentsCollection extends Backbone.Collection
model: School.Models.Student
url: '/students'
class School.Models.Student extends Backbone.Model
paramRoot: 'student'
class School.Routers.StudentsRouter extends Backbone.Router
initialize: (options) ->
@students = new School.Collections.StudentsCollection()
@students.reset options.students
routes:
"index" : "index"
".*" : "index"
index: ->
@view = new School.Views.Students.IndexView(students: @students)
$("#students").html(@view.render().el)
School.Views.Students ||= {}
class School.Views.Students.IndexView extends Backbone.View
template: JST["templates/students/index"]
initialize: () ->
@options.students.bind('reset', @addAll)
addAll: () =>
@options.students.each(@addOne)
addOne: (student) =>
view = new School.Views.Students.StudentView({model : student})
@$("tbody").append(view.render().el)
render: =>
@$el.html(@template(students: @options.students.toJSON() ))
@addAll()
return this
app/assets/javascripts/router/students_router.js.coffee
#= require_self
#= require_tree ../templates
#= require_tree ./models
#= require_tree ./collections
#= require_tree ./views
#= require_tree ./routers
window.School =
Models: {}
Collections: {}
Routers: {}
Views: {}
initialize: (data) -> alert 'Hello for rails'
class School.Collections.StudentsCollection extends Backbone.Collection
model: School.Models.Student
url: '/students'
class School.Models.Student extends Backbone.Model
paramRoot: 'student'
class School.Routers.StudentsRouter extends Backbone.Router
initialize: (options) ->
@students = new School.Collections.StudentsCollection()
@students.reset options.students
routes:
"index" : "index"
".*" : "index"
index: ->
@view = new School.Views.Students.IndexView(students: @students)
$("#students").html(@view.render().el)
School.Views.Students ||= {}
class School.Views.Students.IndexView extends Backbone.View
template: JST["templates/students/index"]
initialize: () ->
@options.students.bind('reset', @addAll)
addAll: () =>
@options.students.each(@addOne)
addOne: (student) =>
view = new School.Views.Students.StudentView({model : student})
@$("tbody").append(view.render().el)
render: =>
@$el.html(@template(students: @options.students.toJSON() ))
@addAll()
return this
app/assets/javascripts/views/students/index_view.js.coffee
#= require_self
#= require_tree ../templates
#= require_tree ./models
#= require_tree ./collections
#= require_tree ./views
#= require_tree ./routers
window.School =
Models: {}
Collections: {}
Routers: {}
Views: {}
initialize: (data) -> alert 'Hello for rails'
class School.Collections.StudentsCollection extends Backbone.Collection
model: School.Models.Student
url: '/students'
class School.Models.Student extends Backbone.Model
paramRoot: 'student'
class School.Routers.StudentsRouter extends Backbone.Router
initialize: (options) ->
@students = new School.Collections.StudentsCollection()
@students.reset options.students
routes:
"index" : "index"
".*" : "index"
index: ->
@view = new School.Views.Students.IndexView(students: @students)
$("#students").html(@view.render().el)
School.Views.Students ||= {}
class School.Views.Students.IndexView extends Backbone.View
template: JST["templates/students/index"]
initialize: () ->
@options.students.bind('reset', @addAll)
addAll: () =>
@options.students.each(@addOne)
addOne: (student) =>
view = new School.Views.Students.StudentView({model : student})
@$("tbody").append(view.render().el)
render: =>
@$el.html(@template(students: @options.students.toJSON() ))
@addAll()
return this
app/assets/templates/students/index.jst.ejs
<h1>Listing students</h1>
<table id="students-table">
<tr>
<th>Name</th>
<th>DOB</th>
<th>Email</th>
</tr>
</table>
列出学生名单
名称
出生日期
电子邮件
html输出
<!DOCTYPE html>
<html>
<head>
<title>School</title>
<link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/students.css?body=1" media="all" rel="stylesheet" type="text/css" />
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery-ui-1.8.18.custom.min.js?body=1" type="text/javascript"></script>
<script src="/assets/underscore.js?body=1" type="text/javascript"></script>
<script src="/assets/json2.js?body=1" type="text/javascript"></script>
<script src="/assets/backbone.js?body=1" type="text/javascript"></script>
<script src="/assets/backbone_rails_sync.js?body=1" type="text/javascript"></script>
<script src="/assets/backbone_datalink.js?body=1" type="text/javascript"></script>
<script src="/assets/backbone-forms.js?body=1" type="text/javascript"></script>
<script src="/assets/school.js?body=1" type="text/javascript"></script>
<script src="/assets/students/edit.js?body=1" type="text/javascript"></script>
<script src="/assets/students/index.js?body=1" type="text/javascript"></script>
<script src="/assets/students/new.js?body=1" type="text/javascript"></script>
<script src="/assets/students/show.js?body=1" type="text/javascript"></script>
<script src="/assets/students/student.js?body=1" type="text/javascript"></script>
<script src="/assets/models/student.js?body=1" type="text/javascript"></script>
<script src="/assets/collections/students.js?body=1" type="text/javascript"></script>
<script src="/assets/views/students/edit_view.js?body=1" type="text/javascript"></script>
<script src="/assets/views/students/index_view.js?body=1" type="text/javascript"></script>
<script src="/assets/views/students/new_view.js?body=1" type="text/javascript"></script>
<script src="/assets/views/students/show_view.js?body=1" type="text/javascript"></script>
<script src="/assets/views/students/student_view.js?body=1" type="text/javascript"></script>
<script src="/assets/routers/students_router.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="46vcYK8H3HIUfZh9wfu+AtzCKs+/2TnESA2ILxhFx0E=" name="csrf-token" />
</head>
<body>
<tbody>
<div id="students">
</div>
</tbody>
</body>
</html>
学校
你有两个问题,看起来既相关又不相关
正常工作,请将以下内容添加到application.html.erb
:
但是,这将使您的代码得以显示,很可能无法使其正确运行 <div id="students"></div>
<script type="text/javascript">
$(function(){
try {
window.SchoolRouter = new School.Routers.StudentsRouter({ students: <%= @students.to_json %> });
backbone.history.start();
}catch(e){
alert(e); // alert so that you see it.
}
});
</script>
$(函数(){
试一试{
window.SchoolRouter=new School.Routers.StudentsRouter({students:});
backbone.history.start();
}捕获(e){
警惕(e);//警惕以便你看到它。
}
});
说到JavaScript,请确保您在浏览器中使用FireBug或开发面板。console.log()是您最好的朋友。将app/views/students/index.html.erb更改为以下代码可以解决问题
$(function(){window.Sepms.initialize({students:});})代码>是否绝对需要使用脚本标记?我意识到这有点像鸡蛋。。。但是,难道没有办法让应用程序以rails资产管道的方式初始化吗#我不知道。如果有的话,对于一个相当简单的问题来说,这似乎是一个不必要的抽象。如果您在rails中使用coffeescript,您可以使用$->
来表示与onDocumentReady()相同的内容。我认为我们可以将/=require-backbone/init
添加到application.js文件中,然后将所有初始化代码放入app/assets/javascript/backbone/init.js
中。这就是我在项目中采用的模式。我想我会添加一个详细的设置描述作为替代答案。