I have the need to fill a dropdown in multiple places, so I made it be its own javascript file (client.js).
When I run the ajax, it fills the array properly, but when I try to import/inject that class into my other class where I need the array, it’s null.
I admit I don’t have the fullest understanding of import/inject, and how that impacts data across classes.
Here’s my code:
client.js:
import jquery from 'jquery';
window.jQuery = jquery;
window.$ = jquery;
export class Client {
constructor()
{
this.client_list = null;
this.valid_client_list();
this.cookie = null;
}
valid_client_list() {
const self = this;
self.cookie = document.cookie.split("=");
self.user = self.cookie[1];
if ( self.user )
{
var url = 'http://aris2.myaris.net/client_list.pl';
jquery.ajax({
type: "GET",
url: url, // URL of the Perl script
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
headers: { 'Access-Control-Allow-Origin': '*' },
mode: 'no-cors',
crossDomain: true,
jsonp: 'callback',
// send username to build client list
data: "REMOTE_USER=" + self.user,
// script call was *not* successful
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("responseText: " + XMLHttpRequest.responseText
+ ", textStatus: " + textStatus
+ ", errorThrown: " + errorThrown);
}, // error
// script call was successful
// data contains the JSON values returned by the Perl script
success: function(data){
if (data.error) { // script returned error
console.log( 'error: ' + data.error );
} // if
else { // got the list
//for (var i = 0; i < data.clients.length; i++ )
//{
// console.log( 'client: ' + i + ' ' + data.clients[i] );
//}
self.client_list = data.clients;
} //else
} // success
}); // ajax
}
return self.client_list;
}
}
and the view where I’m trying to use it:
customer.js:
import './style.css';
//import jquery from 'jquery';
import {Client} from './client';
@inject(Client)
export class Customer {
constructor(client) {
this.selectedClient = 0;
this.client = client;
this.selectedClient = 0;
this.clients = this.client.valid_client_list();
console.log( 'client_list: ' + this.clients );
this.selectedCustInfo = 0;
this.custInfoField = [
{ value: 'All', display: 'All' },
{ value: 'Name', display: 'Name' },
{ value: 'Contacts', display: 'Contacts' },
{ value: 'City', display: 'City' },
{ value: 'My Notes', display: 'My Notes' },
{ value: 'All Notes', display: 'All Notes' },
{ value: 'Report As', display: 'Report As' },
{ value: 'Parent Customer', display: 'Parent Customer' },
];
}
}