angular - Converting AngularJS deffered promise to AngularX observable for JSOM -
i converting angularjs application angular 4 gets data sharepoint. using jsom , executequeryasync() method.
i data executequeryasync() server , store in observable in service. code have transform old version (which not me) below. struggle syntax when converting service. have absolute no plan how convert angular 4.
getallstatusreps($scope) { const deferred = $q.defer(); const ctx = sp.clientcontext.get_current(); const web = ctx.get_web(); const statuslist = web.get_lists().getbytitle(this.statuslistname); const twitterlist = web.get_lists().getbytitle(this.twitterlistname); const statreps = statuslist.getitems(this.getqueryforallstatusreps()); const twitreps = twitterlist.getitems(this.getqueryforallstatusreps()); const querytext = ` <view> <rowlimit>1</rowlimit> <viewfields>{0}</viewfields> <query> <where><isnotnull><fieldref name=\'epmstatusdate\' /></isnotnull></where> <orderby><fieldref name=\'id\' ascending= \'false\' /></orderby> </query> </view>`; const statcamlquery = new sp.camlquery(); statcamlquery.set_viewxml(string.format(querytext, this.getviewfields(true, false))); const laststatrep = statuslist.getitems(statcamlquery); const twitcamlquery = new sp.camlquery(); twitcamlquery.set_viewxml(string.format(querytext, this.getviewfields(false, false))); const lasttwitrep = twitterlist.getitems(twitcamlquery); ctx.load(statreps); ctx.load(twitreps); ctx.load(laststatrep); ctx.load(lasttwitrep); ctx.executequeryasync( function () { deferred.resolve({ statreps: statreps, twitreps: twitreps, laststatrep: laststatrep, lasttwitrep: lasttwitrep }); }, function (sender, args) { deferred.reject('request failed. ' + args.get_message() + '\n' + args.get_stacktrace()); } ); return deferred.promise; }
you can convert promises observables doing:
const $thingwhichyoucansubscribeto = observable.frompromise(serviceorthingthatreturnspromise())
could try @ bottom return promise:
return observable.frompromise(deferred.promise);
then do:
getallstatusreps($scope).subscribe(()=>{...}, ()=>{...})
also re-write using observables , create service returns new observable:
getallstatusreps($scope) { return new observable(observer => { const ctx = sp.clientcontext.get_current(); const web = ctx.get_web(); const statuslist = web.get_lists().getbytitle(this.statuslistname); const twitterlist = web.get_lists().getbytitle(this.twitterlistname); const statreps = statuslist.getitems(this.getqueryforallstatusreps()); const twitreps = twitterlist.getitems(this.getqueryforallstatusreps()); const querytext = ` <view> <rowlimit>1</rowlimit> <viewfields>{0}</viewfields> <query> <where><isnotnull><fieldref name=\'epmstatusdate\' /></isnotnull></where> <orderby><fieldref name=\'id\' ascending= \'false\' /></orderby> </query> </view>`; const statcamlquery = new sp.camlquery(); statcamlquery.set_viewxml(string.format(querytext, this.getviewfields(true, false))); const laststatrep = statuslist.getitems(statcamlquery); const twitcamlquery = new sp.camlquery(); twitcamlquery.set_viewxml(string.format(querytext, this.getviewfields(false, false))); const lasttwitrep = twitterlist.getitems(twitcamlquery); ctx.load(statreps); ctx.load(twitreps); ctx.load(laststatrep); ctx.load(lasttwitrep); ctx.executequeryasync( function () { observer.next({ statreps: statreps, twitreps: twitreps, laststatrep: laststatrep, lasttwitrep: lasttwitrep }); }, function (sender, args) { observer.error('request failed. ' + args.get_message() + '\n' + args.get_stacktrace()); } ); }); }
Comments
Post a Comment