我正在使用 batman.js 作为前端创建一个 rails 3 应用程序。当我在show.html
查看单个记录时,我能够提交成功的删除请求。但是当我在index.html
视图中时,相同的模式不会触发删除方法。我猜当 Batman.js 触发我不理解的删除请求时会发生一些魔法。可能与视图中venue
变量的范围有关。index.html
我将展示我的控制器代码和有问题的两个视图。如果您需要其他东西,请询问。
/app/assets/javascripts/controllers/venues_controller.js.coffee
class Plansandpictures.VenuesController extends Batman.Controller
routingKey: 'venues'
index: (params) ->
@set 'newVenue', new Plansandpictures.Venue
Plansandpictures.Venue.load (err, venues) =>
@set 'venues', Plansandpictures.Venue.get('loaded')
show: (params) ->
Plansandpictures.Venue.find parseInt(params.id, 10), (err,result) =>
throw err if err
@set 'venue', result
@render source: 'venues/show'
create: (params) ->
@newVenue.save (err, venue) =>
if !err
@set 'newVenue', new Plansandpictures.Venue
Plansandpictures.Venue.get('loaded').add(venue)
update: (params) ->
destroy: (params) ->
@get('venue').destroy (err) =>
if err
throw err unless err instanceof Batman.ErrorsSet
else
Plansandpictures.flashSuccess "Removed successfully!"
@redirect '/venues'
/app/assets/javascripts/views/venues/show.html
<h4 data-bind="venue.name"></h4>
<h6>Description</h6>
<p data-bind="venue.description"></p>
<h6>Address</h6>
<p data-bind="venue.address"></p>
<h6>City</h6>
<p data-bind="venue.city"></p>
<h6>State</h6>
<p data-bind="venue.state"></p>
<a data-event-click="controllers.venues.destroy | withArguments venue">delete</a>
/app/assets/javascripts/views/venues/index.html
<h2>Venues</h2>
<table>
<thead>
<th>Name</th>
<th>Description</th>
<th>Address</th>
<th>City</th>
<th>State</th>
</thead>
<tbody>
<tr data-foreach-venue="venues">
<td data-bind="venue.name"></td>
<td data-bind="venue.description"></td>
<td data-bind="venue.address"></td>
<td data-bind="venue.city"></td>
<td data-bind="venue.state"></td>
<td><a data-route="routes.venues[venue]">show</a></td>
<td><a data-event-click="controllers.venues.destroy | withArguments venue">delete</a></td>
</tr>
</tbody>
</table>
<h3>Add a Venue</h3>
<form data-formfor-venue="controllers.venues.newVenue" data-event-submit="controllers.venues.create">
<input class="new-item" placeholder="Name" data-bind="venue.name" />
<input class="new-item" placeholder="Description" data-bind="venue.description" />
<input class="new-item" placeholder="Address" data-bind="venue.address" />
<input class="new-item" placeholder="City" data-bind="venue.city" />
<input class="new-item" placeholder="State" data-bind="venue.state" />
<input class="new-item" type="number" placeholder="1" data-bind="venue.user_id" />
<input type="submit" value="Add Venue" />
</form>