Uncaught model... I think I have async problems?

Here’s my code.

var model = arguments[0].model,&nbsp; &nbsp; row = arguments[0].row;<br>var mA = skuid.$M('AllAppointments');<br>mA.createRow({additionalConditions:[<br>&nbsp; &nbsp; &nbsp; &nbsp; {field: 'Start__c', value: row.Start__c},<br>&nbsp; &nbsp; &nbsp; &nbsp; {field: 'End__c', value: row.End__c},<br>&nbsp; &nbsp; &nbsp; &nbsp; {field: 'Room__c', value: row.Room__c}<br>&nbsp; &nbsp; ]});<br>&nbsp; &nbsp;&nbsp;<br>model.deleteRow(row);<br>mA.save();<br>mA.setCondition(mA.getConditionByName('ThisOne'), mA.getFirstRow().Id);<br>mA.updateData();


I’m getting the error, ‘uncaught model’ on the AllAppoitments model. It looks like the save doesn’t complete before the update is attempted. How do I handle that properly?

Thanks!

Perhaps I found the answer… thanks to Pat for undergoing the agony for the rest of us learners.


Here’s my new code:

var model = arguments[0].model,&nbsp; &nbsp; row = arguments[0].row,&nbsp;<br>&nbsp; &nbsp; $ = skuid.$;<br>var mA = skuid.$M('AllAppointments');<br>mA.createRow({additionalConditions:[<br>&nbsp; &nbsp; &nbsp; &nbsp; {field: 'Start__c', value: row.Start__c},<br>&nbsp; &nbsp; &nbsp; &nbsp; {field: 'End__c', value: row.End__c},<br>&nbsp; &nbsp; &nbsp; &nbsp; {field: 'Room__c', value: row.Room__c}<br>&nbsp; &nbsp; ]});<br>&nbsp; &nbsp;&nbsp;<br>model.deleteRow(row);<br>mA.save();<br>$.when(mA.save())<br>&nbsp; &nbsp;.done(function(result){<br>&nbsp; &nbsp; &nbsp; &nbsp;if (result.totalsuccess) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mA.setCondition(mA.getConditionByName('ThisOne'), mA.getFirstRow().Id);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mA.updateData();<br>&nbsp; &nbsp; &nbsp; &nbsp;} else {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('something went wrong...');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(result);<br>&nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp;})<br>&nbsp; &nbsp;.fail(function(result){<br>&nbsp; &nbsp; &nbsp; &nbsp; console.log('there was a really bad problem, like connecting to the server');<br>&nbsp; &nbsp;});



I typically use this jquery when function. Also, you’d need to do set a deferred promise if there other actions following this in an action framework.


var model = arguments[0]&#46;model, row = arguments[0]&#46;row, $ = skuid&#46;$; var mA = skuid&#46;$M('AllAppointments'); mA&#46;createRow({additionalConditions:[ {field: 'Start__c', value: row&#46;Start__c}, {field: 'End__c', value: row&#46;End__c}, {field: 'Room__c', value: row&#46;Room__c} ]}); model&#46;deleteRow(row); $&#46;when(mA&#46;save()) &#46;done(function(){ mA&#46;setCondition(mA&#46;getConditionByName('ThisOne'), mA&#46;getFirstRow()&#46;Id); mA&#46;updateData(); }) &#46;fail(function(){ console&#46;log('Something went wrong querying ProposalLineItemForCurrentTask model&#46;'); dfdTask&#46;reject(); });<br />

Nice. What does a deferred promise look like?

I’m looking at this: https://community.skuid.com/t/mixing-actions-and-snippets

But I’m not quite sure how to implement.

var model = arguments[0].model, row = arguments[0].row, $ = skuid.$, dfd = new $.Deferred(); var mA = skuid.$M('AllAppointments'); mA.createRow({additionalConditions:[{field: 'Start\_\_c', value: row.Start\_\_c}, {field: 'End\_\_c', value: row.End\_\_c}, {field: 'Room\_\_c', value: row.Room\_\_c}]}); model.deleteRow(row); $.when(mA.save()) .done(function(){ mA.setCondition(mA.getConditionByName('ThisOne'), mA.getFirstRow().Id); $when(mA.updateData()) .done(function(){ dfd.resolve(); }) .fail(function(){ console.log('Something went wrong querying ProposalLineItemForCurrentTask model.'); dfdTask.reject(); }); }) .fail(function(){ console.log('Something went wrong querying ProposalLineItemForCurrentTask model.'); dfdTask.reject(); }); return dfd.promise();

Sweet. Thanks, Pat!

Quite welcome. Those two things, jquery when and deferred, have taken me a long way in working with javascript for skuid purposes.

Hmm…

It seems like the deferred promise isn’t keeping the calendar popup from popping up… https://community.skuid.com/t/calendar-popup-showing-before-deferred-promise-resolved/1699

@skuid employees,


Can we add this to documentation?

“You’d need to do set a deferred promise if there are other actions following this in an action framework.”

Thank you Pat. How did you figure this out? 

I set to learning jquery the hard way. Lots of trial and error. My first post in this thread was the result.