javascript - AngularJS request model before another model create [NodeJS] -
i trying create simple webapp on mean stack.
i want create offer belongs exposition, don't know how request exposition before call createoffer.
here code
$stateprovider .state('offer', { url: "/exposition/:expid/offer/", templateurl: 'app/exposition/listoffers.tpl.html', controller: 'offerscontroller' }) .state('offercreate', { url: "/exposition/:expid/offer/create/", templateurl: 'app/exposition/createoffer.tpl.html', controller: 'offerscontroller' }) .state('offerview', { url: "/exposition/:expid/offer/:id/", templateurl: 'app/exposition/detailsoffer.tpl.html', controller: 'offerscontroller' });
and controller
offerapp.controller('offerscontroller', ['$scope', '$resource', '$state', '$location', 'offerupdateservice', 'upload', function ($scope, $resource, $state, $location, offerupdateservice, upload) { var offerresource = $resource('/offer/:id'); var expositionresource = $resource('/exposition/:id'); $scope.offerupdateservice = new offerupdateservice(); var loadoffers = function () { return offerresource.query(function (results) { $scope.offers = results; if ($state.params.id) { $scope.findoffer($state.params.id); } if ($state.params.expid) { expositionresource.findexposition($state.params.expid); } }); }; } ]);
is correct idea? want load exposition before offer , map exposition.id offer model.
thank you.
you need use promise chain this. i'm not entirely clear how resource objects work (as use $q & $http instead, docs indicate return objects restful apis). here example of requesting 2 resources in sequential order:
var offerresource = $resource('/offer/:id'); var expositionresource = $resource('/exposition/:id'); expositionresource.get({id:123}).$promise.then( function(rsp, rspheaders){ //set model id how want model.id = rsp.id //now hit next api in sequence offerresource.query({id:model.id}).$promise.then( function(rsp2, rspheaders2){ //you can again if need 3rd sequential call }) })
Comments
Post a Comment