我有一个播放列表和一个跟踪模型,playlist
hasMany
tracks
。
我尝试删除与给定控制器关联的模型中的所有轨道,然后再添加下一次搜索的结果。
Pg.SearchController = Ember.ObjectController.extend
queryTerm: ''
actions:
search: ->
apiKey = "__API_KEY__"
queryTerm = @get('queryTerm')
@set 'model.title', "Search: #{queryTerm}"
$.ajax "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&q=#{queryTerm}&key=#{apiKey}"
.then (response) =>
model = @get('model')
# Remove all tracks from playlist here.
model.get('tracks').forEach (track) =>
model.get('tracks').removeObject track
for video in response.items
track = @store.createRecord 'track',
identifier: video['id']['videoId']
title: video['snippet']['title']
model.get('tracks').pushObject track
我尝试了几种方法,但他们都在关系中留下了一些记录。如何在不破坏对象本身的情况下从hasMany
关系中删除所有记录。
以下是我的尝试:
将removeObject
与forEach
一起使用:
model.get('tracks').forEach (track) =>
model.get('tracks').removeObject track
使用for..in:
for track in model.get('tracks')
model.get('tracks').removeObject track
直接在关系中使用removeObjects
:
tracks = model.get('tracks.content')
model.get('tracks').removeObjects tracks