关于angular的$resource中的isArray属性问题
在之前的文章中講到了在使用$resource的時候,有一個isArray屬性. 這個屬性在兩個地方有提到:
1. angular學習筆記(二十八)-$http(6)-使用ngResource模塊構建RESTful架構
$resource的四個方法: query方法,get方法,save方法,remove方法,delete方法
這四個方法里,唯獨query方法,它的isArray屬性是true,所以,query接受到的數據是數組,而其余四個方法,接收到的數據必須不能是數組
2.?angular學習筆記(二十八-附1)-$resource中的資源的方法
$resource的自定義方法: 比如該篇的栗子中使用的$charge方法,在定義自定義方法的時候,也要指定isArray屬性.不指定的話默認是false.
?
如果我們不設置isArray為true,但是后臺卻給它返回一個數組資源,是會報錯的.
那么,如果在使用get方法,save方法,remove方法,delete方法的時候,需要返回數組對象呢? 答案是不能的.因為$resource這個服務沒有提供相應的配置方法.
那么,如果自定義一個方法,設置isArray為true,是不是就可以接受數組格式的返回值了呢? 答案是,要看情況!?
比如下面這段代碼:
var notePad = angular.module('notePad',['ngResource']); notePad.factory('myNotes',['$resource',function($resource){return $resource('/notes/:id',{id:'@id'},{mysave:{method:'POST',isArray:true}}) }]); notePad.factory('loadNotes',['myNotes','$q',function(myNotes,$q){return function(){var defer = $q.defer();myNotes.query(function(notes){defer.resolve(notes);},function(err){defer.reject(err)});return defer.promise} }]); notePad.factory('loadNote',['myNotes','$q',function(myNotes,$q){return function(noteId){var defer = $q.defer();myNotes.get({id:noteId},function(note){defer.resolve(note);},function(err){defer.reject(err)});return defer.promise} }]); notePad.directive('notepad',['loadNotes','myNotes',function(loadNotes,myNotes){return {restrict:'EA',templateUrl:'template.html',scope:{},link:function(scope,iEle,iAttr){scope.editMode = false;scope.curText = '';scope.ifNew = true;loadNotes().then(function(data){scope.notes=data},function(data){});var editBox = iEle.find('.edit');editBox.bind('keydown keyup',function(){scope.curText = $(this).html()});scope.editNote = function(id){scope.editMode = true;if(id != undefined){scope.curText = scope.notes[id]['content']}else {scope.curText = '';}};scope.saveNote = function(){scope.editMode = false;if(scope.ifNew){var newNote = new myNotes();newNote.content = scope.curText;newNote.title = scope.curText.length>5 ? scope.curText.substring(0,5)+'...' : scope.curText;newNote.id = scope.notes.length;newNote.$mysave(function(data){console.log(data)})}}}} }]);內容比較多,只看關鍵部分代碼:
1.myNotes服務通過$resource創建$resource()對象,然后給它添加自定義的mysave方法,設置isArray屬性為false.
2.實例化一個newNote資源,然后調用自定義的$mysave方法
后臺我們給它返回一個數組對象:
var notes = [{'title':'吃飯','content':'吃飯啊','id':0 },{'title':'睡覺','content':'睡覺啊','id':1 },{'title':'喂兔子','content':'喂兔子啊','id':2 }]; app.post('/notes/:id',function(req,res){var noteId = req.params.id;notes[noteId] = req.body;res.send(notes) });結果報錯:?
這個很好理解,因為我們設置了isArray是false嘛.
然后我把isArray屬性改為true:
結果還是報錯:
百思不得其解后去看了angular-resource的源碼,發現里面有這樣一段:
var isInstanceCall = this instanceof Resource; var value = isInstanceCall ? data : (action.isArray ? [] : new Resource(data)); if (action.isArray) {value.length = 0;forEach(data, function (item) {if (typeof item === "object") {value.push(new Resource(item));} else {// Valid JSON values may be string literals, and these should not be converted// into objects. These items will not have access to the Resource prototype// methods, but unfortunately there value.push(item);}});}注意這里的value對象,并非設置isArray為true時,它就是[],首先要求isInstanceCall是false,然后再判斷isArray...
所以,這里的newNote是$resource()的實例,這樣,isArray無論如何配置,它都不能接受數組格式的返回數據.并且必須配置為false.
所以這里應該這樣做:
newNote.$mysave(function(data){console.log(data)})//改為: myNotes.mysave(newNote,function(data){console.log(data)});
不要使用資源實例的方法,而是直接使用$resource()的方法.這樣就可以正確接收數組格式的返回值了.
angular這樣設計,應該是符合RESTful架構風的(我猜的),因為提交一個資源,那么返回的也應該是一個資源,而不應該是整個數組,所以應該盡量避免這種做法.這里只是我自己做練習的時候遇到這樣的情況.
完整代碼參考:?angular指令實戰-notepad
?
總結
以上是生活随笔為你收集整理的关于angular的$resource中的isArray属性问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: KVM硬件辅助虚拟化之 EPT(Exte
- 下一篇: 数论 - 简单数位推理 --- NYIS