javascript - AngularJS example in $resource API -


http://docs.angularjs.org/api/ngresource.$resource

in above link, there example:

// define creditcard class var creditcard = $resource('/user/:userid/card/:cardid',  {userid:123, cardid:'@id'}, {   charge: {method:'post', params:{charge:true}}  });  // can retrieve collection server var cards = creditcard.query(function() {   // get: /user/123/card   // server returns: [ {id:456, number:'1234', name:'smith'} ];    var card = cards[0];   // each item instance of creditcard   expect(card instanceof creditcard).toequal(true);   card.name = "j. smith";   // non methods mapped onto 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 mapped well.   card.$charge({amount:9.99});   // post: /user/123/card/456?amount=9.99&charge=true {id:456, number:'1234', name:'j. smith'} });  // can create instance 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); 

there line:

var card = cards[0]; 
  1. i have no idea array cards coming from. match found @ previous line , doesn't make sense me variable outside of function scope.

  2. for jasmine expect function, angular run , throw error?

there lines function expect(), such as:

expect(card instanceof creditcard).toequal(true); 

i know jasmine testing function, wonder if browser/angular directly runs don't see jasmine library in code.

to answer question 1. line referring (var card = cards[0];) occurring inside success callback of query method. means has executed, in turn means cards variable populated. cards variable not outside of scope, can referenced inside callback function, , other function matter. typically not use cards variable outside scope of success callback, it's asynchronous method.

please elaborate on question 2?


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -