Hello everyone,
I'm new to customizing the MPOS (D365 Commerce) and I'm trying to fill the "Select" control with data from the database. I have validated that my controller (Api) returns the required information, however it is not reflected in the view (Cart).
import {
CartViewCustomControlBase,
ICartViewCustomControlState,
ICartViewCustomControlContext,
CartChangedData
} from "PosApi/Extend/Views/CartView";
import {
ObjectExtensions,
StringExtensions,
ArrayExtensions
} from "PosApi/TypeExtensions";
import { ProxyEntities, ClientEntities } from "PosApi/Entities";
import { TipoComprobanteApiPE } from "../../DataService/DataServiceRequests.g";
import { Entities } from "../../DataService/DataServiceEntities.g";
import MessageDialog from "../../DialogSample/MessageDialog";
export default class SalesCustomControl extends CartViewCustomControlBase {
private _cart: Observable<ProxyEntities.Cart>;
public documentTypeIdPE: Observable<string>;
public documentTypeListPE: ObservableArray<Entities.TipoComprobantePEEntity>;
private _context: ICartViewCustomControlContext;
private _state: ICartViewCustomControlState;
private static readonly TEMPLATE_ID: string = "Microsoft_Pos_Extensibility_Samples_Sales";
public readonly documentTypePELabel: string;
constructor(id: string, context: ICartViewCustomControlContext) {
super(id, context);
/* Start fixed values*/
let tp1: Entities.TipoComprobantePEEntity = new Entities.TipoComprobantePEEntity();
tp1.TipoComprobanteId = "-";
tp1.TipoComprobante = "--DEFAULT--";
let tp2: Entities.TipoComprobantePEEntity = new Entities.TipoComprobantePEEntity();
tp2.TipoComprobanteId = "01";
tp2.TipoComprobante = "INVOICE";
/* End fixed values*/
this._context = context;
this.documentTypeIdPE = ko.observable("");
this.documentTypeListPE = ko.observableArray([tp1, tp2]);
this.documentTypePELabel = this._context.resources.getString("string_50");
this._cart = ko.observable(null);
this.cartChangedHandler = (data: CartChangedData) => {
this._cart(data.cart);
};
this.documentTypeIdPE.subscribe((newValue: string): void => {
this._addOrUpdateExtensionProperty("DOCUMENTTYPEID", <ProxyEntities.CommercePropertyValue>{ StringValue: newValue });
});
}
public onReady(element: HTMLElement): void {
ko.applyBindingsToNode(element, {
template: {
name: SalesCustomControl.TEMPLATE_ID,
data: this
}
});
}
public init(state: ICartViewCustomControlState): void {
this._state = state;
let correlationIdDT: string = this._context.logger.getNewCorrelationId();
let requestTP: TipoComprobanteApiPE.GetTipoComprobanteRequest<TipoComprobanteApiPE.GetTipoComprobanteResponse> = new TipoComprobanteApiPE.GetTipoComprobanteRequest();
this._context.runtime.executeAsync(requestTP)
.then((result: ClientEntities.ICancelableDataResult<TipoComprobanteApiPE.GetTipoComprobanteResponse>): void => {
if (!(result.canceled || ObjectExtensions.isNullOrUndefined(result.data))) {
let myResult: Entities.TipoComprobantePEEntity[] = [];
result.data.result.forEach((nRow: Entities.TipoComprobantePEEntity): void => {
myResult.push(nRow);
//It returns all values that I expect.
});
this.documentTypeListPE(myResult);
}
})
.catch((reason: any) => {
this._context.logger.logError(JSON.stringify(reason), correlationIdDT);
});
}
/**
* Gets the property value from the property bag, by its key. Optionally creates the property value on the bag, if it does not exist.
*/
private _addOrUpdateExtensionProperty(key: string, newValue: ProxyEntities.CommercePropertyValue): void {
let cart: ProxyEntities.Cart = this._cart();
let extensionProperty: ProxyEntities.CommerceProperty = Commerce.ArrayExtensions.firstOrUndefined(cart.ExtensionProperties, (property: ProxyEntities.CommerceProperty) => {
return property.Key === key;
});
if (ObjectExtensions.isNullOrUndefined(extensionProperty)) {
let newProperty: ProxyEntities.CommerceProperty = {
Key: key,
Value: newValue
};
if (ObjectExtensions.isNullOrUndefined(cart.ExtensionProperties)) {
cart.ExtensionProperties = [];
}
cart.ExtensionProperties.push(newProperty);
} else {
extensionProperty.Value = newValue;
}
this._cart(cart);
}
}
***********************************************************************
If I add fixed values to my variable "documentTypeListPE" it shows as I expected.
I will be grateful for the help you can give me.
thanks in advance.