我试图删除行的表骨干。我使用的Rails 4和骨干,上轨宝石为这个项目。
I'm trying to remove a row in a table with Backbone. I'm using Rails 4 and the backbone-on-rails gem for this project.
我使用的是addEntryevent在我的电影表中添加一行。这增加了包含ID,MOVIE_TITLE和user_ID的一个新行。
I'm using a addEntryevent to add row in my Movies table. This adds a new row containing a id, movie_title and user_id.
该索引视图
class Movieseat.Views.MovieseatsIndex extends Backbone.View template: JST['movieseats/index'] initialize: -> @collection.on('update', @render, this) @collection.on('add', @appendEntry, this) render: -> $(@el).html(@template()) @collection.each(@appendEntry) this events: -> "click li": "addEntry" "click .remove": "removeEntry" addEntry: (e) -> movie_title = $(e.target).text() @collection.create title: movie_title removeEntry: (e) -> thisid = @$(e.currentTarget).closest('div').data('id') console.log thisid @collection.remove thisid appendEntry: (entry) -> view = new Movieseat.Views.Entry(model: entry) $('#entries').append(view.render().el)当我点击卸下摆臂元素上,我得到示出存储在我想删除该元素的ID的控制台日志的结果。
When I click on the .remove element I get a console log result showing the id stored in the element I want to remove.
该条目来查看
class Movieseat.Views.Entry extends Backbone.View template: JST['movieseats/entry'] className: 'movie-frame' render: -> $(@el).html(@template(entry: @model)) this这是我的index.jst.eco模板
This is my index.jst.eco template
<h1>Movies</h1> <div id="entries"></div>这是项模板,我在index.jst.eco模板(使我这样做,所以我只能重新呈现加入到视图的电影电影
This is the Entry template I render in the index.jst.eco template (i'm doing this so I only rerender the movie movies added to to the view.
<div class="movie-frame" data-id="<%= @entry.get('id') %>"> <p><%= @entry.get('title') %></p> <p class="remove">Remove</p> </div>和我的骨干线路,
class Movieseat.Routers.Movieseats extends Backbone.Router routes: '': 'index' initialize: -> @collection = new Movieseat.Collections.Movieseats() @collection.fetch() index: -> view = new Movieseat.Views.MovieseatsIndex(collection: @collection) $('#container').html(view.render().el)但还有当我点击卸下摆臂元素的网络活动。有网络活动时的addEntry事件triggerd。
But there's no network activity when I click on the .remove element. There is network activity when the addEntry event is triggerd.
推荐答案所以显然 @ collection.remove 不熄一个HTTP请求。 @ collection.get将其更改为(thisid).destroy()的伎俩。但是,你仍然需要在你的Rails控制器创建销毁方法,
So apparently @collection.remove doesn't put out a HTTP request. Changing this to @collection.get(thisid).destroy() does the trick. But you will still need to create a destroy method in your Rails controller,
在骨干视图索引的removeEntry事件,
The removeEntry event in the Backbone view index,
removeEntry: (e) -> thisid = @$(e.currentTarget).closest('div').data('id') @collection.get(thisid).destroy()在Rails控制器的destroy方法,
The destroy method in the Rails controller,
def destroy movie = Movie.find_by_id(params[:id]) movie.destroy respond_to do |format| format.json {render :json => {:msg => "item deleted!"},:status => 200} end end更多推荐
主干由ID删除一行
发布评论