Angularjs承诺对象覆盖属性

时间:2014-11-25 09:24:22

标签: javascript angularjs

我在服务中有两个相关的模型,然后有一个外部事件触发带有添加选项的重新查询。

服务Surah有很多Ayahs:

.service 'Surah', (Restangular, Ayah) ->

    class Surah
      constructor: (obj) ->
        for key of obj
          @[key] = obj[key]

      getAyahs: (from, to) ->
        self = @
        @ayahs = Ayah.all
          surah_id: @id, from: from, to: to
        .then (ayahs) ->
          console.log(ayahs)
          self.ayahs = ayahs
      @new: (id)->
        return Restangular.one('surahs', id).get().then (data)->
          return new Surah(data)

      @all: ->
        return Restangular.all("surahs").getList()



.service 'Ayah', (Restangular, userOptions, utils) ->
    # AngularJS will instantiate a singleton by calling "new" on this function
    class Ayah
      constructor: (obj)->
        for key of obj
          @[key] = obj[key]

      @all: (hash) ->
        Restangular.one('surahs', hash.surah_id).getList 'ayat', 
          content: userOptions.content.join(","), quran: userOptions.quran, audio: userOptions.audio, from: hash.from, to: hash.to
        .then (data)->
          fontFaces = []
          console.log(data)
          ayahs = data.map (ayah) ->
            # The ayah object is returned from Restangular and given all the properties that restangular gives it. For example, you can call
            # ayah.save() or ayah.remove() which will make API calls accordingly. This is power and will be perserved when creating the Ayah object
            return new Ayah(ayah)

          utils.createFontFaces(ayahs)
          return ayahs

在我的控制器中,根据选项更改,我对.all()服务中Ayah的另一个查询进行了查询,该查询位于Surah服务的数组和属性中。想象一下:

{ayahs: [A,A,A,A,A,A]}

一旦我打电话,在控制器中,我得到一个Promise对象,然后阵列填充新的Ayahs。这对用户来说不是一种有趣的体验,因为字面上数组中的所有内容都会消失,然后重新填充。我怎么能避免这种情况?我怎么能等待承诺返回然后更新包含所有Ayahs的数组?

谢谢!

0 个答案:

没有答案