主干由ID删除一行

编程入门 行业动态 更新时间:2024-10-09 23:17:46
本文介绍了主干由ID删除一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图删除行的表骨干。我使用的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删除一行

本文发布于:2023-11-22 00:29:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615276.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主干   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!