Javascript Angularjs处理承诺和模型
我想知道解决angularjs模型和承诺模型之间的许多关系的最佳方法是什么。请允许我举一个例子,我希望您能提供反馈,告诉我是否有更好的方法来实现这一点 一个Javascript Angularjs处理承诺和模型,javascript,angularjs,coffeescript,Javascript,Angularjs,Coffeescript,我想知道解决angularjs模型和承诺模型之间的许多关系的最佳方法是什么。请允许我举一个例子,我希望您能提供反馈,告诉我是否有更好的方法来实现这一点 一个Surah有许多Ayahs 我的Surah车型: angular.module('qurancomApp') .service 'Surah', (Restangular, userOptions, Ayah) -> class Surah constructor: (obj) -> fo
Surah
有许多Ayahs
我的Surah
车型:
angular.module('qurancomApp')
.service 'Surah', (Restangular, userOptions, 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) ->
self.ayahs = ayahs
@new: (id)->
return Restangular.one('surahs', id).get().then (data)->
return new Surah(data)
@all: ->
return Restangular.all("surahs").getList()
angular.module('qurancomApp')
.service 'Ayah', (Restangular, userOptions) ->
# 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, quran: userOptions.quran, audio: userOptions.audio
.then (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)
return ayahs
MyAyah
车型:
angular.module('qurancomApp')
.service 'Surah', (Restangular, userOptions, 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) ->
self.ayahs = ayahs
@new: (id)->
return Restangular.one('surahs', id).get().then (data)->
return new Surah(data)
@all: ->
return Restangular.all("surahs").getList()
angular.module('qurancomApp')
.service 'Ayah', (Restangular, userOptions) ->
# 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, quran: userOptions.quran, audio: userOptions.audio
.then (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)
return ayahs
以及相应的控制器:
angular.module('qurancomApp')
.controller 'AyahsCtrl', ($scope, $routeParams, Surah) ->
rangeArray = $routeParams.range.split("-")
Surah.new($routeParams.id).then (surah) ->
$scope.currentSurah = surah
console.log $scope.currentSurah.getAyahs(rangeArray[0], rangeArray[1])
实际上,问题是:在控制器中,我调用函数Surah.new()
,然后继续从后端获取Surah
,然后想要创建它的关联Ayahs
,我将Ayah
模型注入Surah
模型,并在Surah
模型中对正在Surah
模型中消化的Ayah
模型做出承诺,在Surah.new()
函数的总体承诺范围内
这样做正确吗?有没有更好的办法 可能会有一个参数来执行一个API调用,该调用将返回您所需的所有内容,但另一种方法有令人信服的理由。没有一般规则。有关在此处组合API请求的更多详细信息: 假设API如您所述,在加载Ayahs之前响应对Surah的第一个调用可能会有好处,但同样,也有令人信服的理由以另一种方式进行响应 如果Surah中有不依赖于Ayah的有用信息,您可以将其呈现,然后每次加载Ayah时,也将其呈现到页面中(这需要更加小心地进入界面,适当时显示加载微调器,优雅地处理错误等) 如果信息只有在全部加载时才有用,那么你现在拥有它的方式对我来说是非常合理的