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

  • 2
  • Problem
  • Updated 2 years ago
Here's my code.

var model = arguments[0].model,    row = arguments[0].row;
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);
mA.save();
mA.setCondition(mA.getConditionByName('ThisOne'), mA.getFirstRow().Id);
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!
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb

Posted 4 years ago

  • 2
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
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,    row = arguments[0].row, 
    $ = skuid.$;
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);
mA.save();
$.when(mA.save())
   .done(function(result){
       if (result.totalsuccess) {
            mA.setCondition(mA.getConditionByName('ThisOne'), mA.getFirstRow().Id);
            mA.updateData();
       } else {
          console.log('something went wrong...');
          console.log(result);
       }
   })
   .fail(function(result){
        console.log('there was a really bad problem, like connecting to the server');
   });
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
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].model,    row = arguments[0].row, 
    $ = skuid.$;
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);
		mA.updateData();
	})
	.fail(function(){

                console.log('Something went wrong querying ProposalLineItemForCurrentTask model.');
                	dfdTask.reject();

	});
(Edited)
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Nice. What does a deferred promise look like?
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb


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();
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Sweet. Thanks, Pat!
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
Quite welcome. Those two things, jquery when and deferred, have taken me a long way in working with javascript for skuid purposes.
Photo of Shmuel Kamensky

Shmuel Kamensky, Champion

  • 4,328 Points 4k badge 2x thumb
Thank you Pat. How did you figure this out? 
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
I set to learning jquery the hard way. Lots of trial and error. My first post in this thread was the result.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Hmm...

It seems like the deferred promise isn't keeping the calendar popup from popping up... https://community.skuidify.com/skuid/topics/calendar-popup-showing-before-deferred-promise-resolved
Photo of Shmuel Kamensky

Shmuel Kamensky, Champion

  • 4,328 Points 4k badge 2x thumb
@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."