http://docs.angularjs.org/api/ngResource .$resource
在上面的链接中,有一个例子:
// Define CreditCard class
var CreditCard = $resource('/user/:userId/card/:cardId',
{userId:123, cardId:'@id'}, {
charge: {method:'POST', params:{charge:true}}
});
// We can retrieve a collection from the server
var cards = CreditCard.query(function() {
// GET: /user/123/card
// server returns: [ {id:456, number:'1234', name:'Smith'} ];
var card = cards[0];
// each item is an instance of CreditCard
expect(card instanceof CreditCard).toEqual(true);
card.name = "J. Smith";
// non GET methods are mapped onto the instances
card.$save();
// POST: /user/123/card/456 {id:456, number:'1234', name:'J. Smith'}
// server returns: {id:456, number:'1234', name: 'J. Smith'};
// our custom method is mapped as well.
card.$charge({amount:9.99});
// POST: /user/123/card/456?amount=9.99&charge=true {id:456, number:'1234', name:'J. Smith'}
});
// we can create an instance as well
var newCard = new CreditCard({number:'0123'});
newCard.name = "Mike Smith";
newCard.$save();
// POST: /user/123/card {number:'0123', name:'Mike Smith'}
// server returns: {id:789, number:'01234', name: 'Mike Smith'};
expect(newCard.id).toEqual(789);
有一行:
var card = cards[0];
我不知道数组
cards
来自哪里。我发现的唯一匹配是在上一行,这对我来说没有意义,因为它是函数范围之外的变量。对于 jasmine expect 函数,Angular 真的会运行它并抛出错误吗?
有一些带有 function 的行expect()
,例如:
expect(card instanceof CreditCard).toEqual(true);
我知道这是一个 Jasmine 测试功能,但我想知道浏览器/Angular 是否直接运行它,因为我在代码中没有看到任何 Jasmine 库。