Any reason that .get would not work on a front end...
# suitecommerce
i
Any reason that .get would not work on a front end SCModel? If I log that model it shows that it has an attribute but when I try and do model.get it returns undefined
s
The only thing that comes to mind is that you're not correctly spelling the attribute name, eg not camelcasing when you should
Does a simple
get()
work, without passing in the name property name?
i
I've verified the name is spelled correctly. When I just do get() it says undefined as well
s
if model.get() returns undefined then it probably means you're calling it before it has had time to set its values
Has the fetch completed when you call it?
i
Yes, I am logging the model before calling .get() and it has all of its attributes.
Here is the log of the model and then the undefined .get right underneath
s
And can you share your code?
i
// Model.js
// -----------------------
// @module Case
define("Urb.UrbanSurfacesMods.BackendTesting.Model", ["SCModel", "Utils"], function(
    
SCModelModule,
    
Utils
) {
    
"use strict";
    
// @class Case.Fields.Model @extends Backbone.Model
    
var SCModel = SCModelModule.SCModel;
    
function BackEndTestingModel () {
        
SCModel.call(this);
        
this.urlRoot = function() {
            
return Utils.getAbsoluteUrl(
                
getExtensionAssetsPath(
                    
"Modules/BackendTesting/SuiteScript2/Urb.BackendTesting.Service.ss"
                
), true
            
)
        
}
    
}
    
BackEndTestingModel.prototype = Object.create(SCModel.prototype);
    
BackEndTestingModel.prototype.constructor = BackEndTestingModel;
    
return BackEndTestingModel
});
s
And where are you logging its values?
i
This is the entry point for a myaccount module
define(
    
'Urb.MyAccountMods.MyAccountsModsModule'
    
, [
        
'Urb.UrbanSurfacesMods.BackendTesting.Model'
    
]
    
,   function (
        
BackEndTestingModel
    
) {
            
return {
                
mountToApp: function mountToApp(container) {
                    
var layout = container.getComponent('Layout')
                    
,   userProfile = container.getComponent('UserProfile');
                    
//Set pay invoices button link
                    
if (layout && userProfile) {
                        
layout.on('afterShowContent', function() {
                            
var backEndModel = new BackEndTestingModel();
                            
backEndModel.fetch();
                            
console.dir(backEndModel);
                            
var backEndMessage = backEndModel.get("coolMessage");
                            
console.log(backEndMessage);
                            
console.log('Passed it');
                            
var userProfileData = userProfile.getUserProfile().then(function(profile) {
                                
console.dir(profile);
                                
var customerRecordInternalId = profile.internalid;
                                `var payStatementsLink = `

https://checkout.paystand.com/v4/?publishableKey=yz312co01spfqbx7uwwo1rvj&viewCheckout=portal-medium&module=ns_openInvoices&extCustomerId=${customerRecordInternalId}&externalCss=https:%2F%2Fwww.paystand.com%2Fhubfs%2Fcustom_css%2FUrbanSurfaces%2FUrbanSurfaces.css&logoUrl=https:%2F%2Fwww.paystand.com%2Fhubfs%2Fcustom_css%2FUrbanSurfaces%2Furban_surfaces.png▾

`;`                                 
$('#payinvoicesbutton').attr('href',payStatementsLink);
                            
});
                        
});
                    
}
                
}
            
}
    
}
)
s
Yeah, this what I was saying earlier. You're logging the value of the model before the fetch has completed
The fetch returns a deferred object and if you want to execute code after it has completed, you will need to chain a
.then()
to the fetch
Alternatively, you can set the fetch promise to a variable and then use
jQuery.when()
on it
i
Ooooooh.... makes sense now! Thank you for helping me with that. I will check that article out.
m
Tip: Always return and use
jQuery.Deferred().promise();