Skip to content

Instantly share code, notes, and snippets.

@Mozu-CS
Last active March 15, 2016 16:26
Show Gist options
  • Save Mozu-CS/a6c7284010f162fb7ee0 to your computer and use it in GitHub Desktop.
Save Mozu-CS/a6c7284010f162fb7ee0 to your computer and use it in GitHub Desktop.
A code snippet demonstrating how to create a custom MZDB entity list that is visible in Storefront.
api.get('entity', { "listName": "storelocations@grobelny", "id": "Nordstrom's"})
.then(function(result) {
console.log(result.data);
console.log(result.data.storeName);
console.log(result.data.latitude);
console.log(result.data.longitude);
});
//For more info on API calls from Storefront, see this documentation:
//https://github.com/Mozu/mozu-storefront-sdk/blob/master/docs/sdkreference.md
var FiddlerProxy = require('mozu-node-sdk/plugins/fiddler-proxy');
var apiContext = require('mozu-node-sdk/clients/platform/application')();
apiContext.plugins = [FiddlerProxy()];
var entityListResource = require('mozu-node-sdk/clients/platform/entityList')(apiContext);
var entityResource = require('mozu-node-sdk/clients/platform/entitylists/entity')(apiContext);
var documentResource = require('mozu-node-sdk/clients/content/documentlists/document')(apiContext);
var entityListStoreLocation = {
//tenant or site is acceptable here.
"contextLevel": "tenant",
//The idProperty must be a unique identifier for each entity in the entity list.
//You can omit this value if you want to use the auto-generated hashes as ids for each entity.
"idProperty": {
"dataType": "string",
"propertyName": "storeName"
},
//While there are only 4 indexable fields in an entity list, you can have as many values within each entity.
//Setting this value as an index will allow you to filter on this value in the entity list.
"indexA": {
"dataType": "string",
"propertyName": "storeName"
},
"isSandboxDataCloningSupported": true,
"isShopperSpecific": false,
//This must be set to true if you need to access the data in this list from Storefront.
"isVisibleInStorefront": true,
//This is the name of the entity list. You'll combine this with namespace to query this list from Storefront.
//The full name of this entity list would be storelocations@grobelny
"name": "storelocations",
//Namespace must match with the custom application namespace that creates this entity list.
//You can find namespaces within the application key for the application.
//For example:
//Grobelny.MDCID_AM.1.0.0.Release
//Grobelny is the namespace for this application.
"nameSpace": "grobelny",
"useSystemAssignedId": false
};
//Call to create the entity list.
entityListResource.createEntityList({}, { body: entityListStoreLocation })
.then(function(entityListCollection) {
console.info(entityListCollection);
//Once the entity list is created, you can add entities to the list.
var entityNordstrom = {
"storeName": "Nordstrom's",
"latitude": "30.386982399999997",
"longitude": "-97.7083469"
};
return entityResource.insertEntity({ entityListFullName: "storelocations@grobelny" }, { body: entityNordstrom });
})
.then(function(result) {
console.log(result);
//This entity editor has three xtypes of textfield that correlate to storeName, latitude, and longitude.
//You can view the unstringified code for the Ext object in here in this Gist
var entityEditor = {
"documentTypeFQN": "entityEditor@mozu",
"listFQN": "entityEditors@mozu",
//This name is displayed when viewing in the list of entity editors
"name": "store-location-editor",
"properties": {
"code": "Ext.create('Ext.form.Panel', {\r\n title: 'Store Location Editor',\r\n\r\n\r\n // Fields will be arranged vertically, stretched to full width\r\n layout: 'anchor',\r\n defaults: {\r\n anchor: '100%'\r\n },\r\n\r\n // The fields\r\n defaultType: 'textfield',\r\n items: [\r\n {\r\n fieldLabel: 'Store Name',\r\n name: 'storeName',\r\n xtype: 'textfield',\r\n }, {\r\n fieldLabel: 'Latitude',\r\n name: 'latitude'\r\n },\r\n {\r\n fieldLabel: 'Longitude',\r\n name: 'longitude'\r\n }\r\n ],\r\n\r\n\r\n setData: function (data) {\r\n\r\n this.getForm().setValues(data);\r\n this.data = data;\r\n },\r\n getData: function () {\r\n\r\n var data = this.getValues(false, false, false, true);\r\n\r\n return Ext.applyIf(data, this.data);\r\n\r\n },\r\n\r\n\r\n});",
//Here, you add the entity lists that this editor will qualify for
"entityLists": ["storelocations@grobelny"],
"priority": 0
}
};
//Create the entity editor
documentResource.createDocument({ documentListName: "entityEditors@mozu" }, { body: entityEditor });
})
.then(function(result) {
console.log(result);
})
.catch(function(err) {
console.error(err);
});
Ext.create('Ext.form.Panel', {
title:false,
layout: 'anchor',
defaults: {
anchor: '100%'
},
// The fields
defaultType: 'textfield',
items: [],
setData: function (data) {
Ext.Object.each(data, function (k, v) {
var editor;
if (Ext.isArray(v)) {
editor = {
xtype: 'boxselect',
fieldLabel: k,
name: k,
store: [],
value: v,
"queryMode": 'local',
"forceSelection": false,
"createNewOnEnter": true,
"createNewOnBlur": true
};
} else if (!Ext.isObject(v)) {
editor = {
xtype: 'textfield',
name: k,
value: v,
fieldLabel: k
};
}
if (editor) {
if (Ext.isArray(this.items)) {
this.items.push(editor);
} else {
this.add(editor);
}
}
}, this);
this.data = data;
},
getData: function () {
var data = this.getValues(false, false, false, true);
return Ext.applyIf(data, this.data);
}
});
{
"documentTypeFQN": "entityEditor@mozu",
"listFQN": "entityEditors@mozu",
"name": "store-location-editor",
"properties": {
"code": "Ext code goes here",
"documentLists": [],
"documentTypes": [],
"entityLists": ["storelocations@grobelny"],
"priority": 0
}
//This Ext data creates the UI portion when editing and creating new entities in the entity list.
//Some of the Ext functionality Mozu exposes is still undocumented.
//It's a good idea to investigate existing entity editors when creating a new one.
Ext.create('Ext.form.Panel', {
title: 'Store Location Editor',
// Fields will be arranged vertically, stretched to full width
layout: 'anchor',
defaults: {
anchor: '100%'
},
// The fields
defaultType: 'textfield',
items: [
{
fieldLabel: 'Store Name',
//storeName also acts as the ID for each entity in the storelocations entity list.
//We set that up in the entity list definition
name: 'storeName',
//You can declare an xtype explictly
xtype: 'textfield',
}, {
fieldLabel: 'Latitude',
//This value must correlate to the MZDB entity that you are editing.
name: 'latitude'
//Or leave the xtype blank and rely on the default type
},
{
fieldLabel: 'Longitude',
name: 'longitude'
}
],
setData: function (data) {
this.getForm().setValues(data);
this.data = data;
},
getData: function () {
var data = this.getValues(false, false, false, true);
return Ext.applyIf(data, this.data);
},
});
Ext.create('Ext.form.Panel', {
title: 'Sub Nav Link Editor',
requires: ['Ext.data.UuidGenerator', 'Ext.window.MessageBox'],
// Fields will be arranged vertically, stretched to full width
layout: 'anchor',
defaults: {
anchor: '100%'
},
// The fields
defaultType: 'textfield',
items: [
{
fieldLabel: 'parentId',
xtype: 'combobox',
name: 'parentId',
queryMode: 'local',
store: ['customers', 'catalog', 'orders', 'marketing', 'sitebuilder', 'settings', 'locations', 'publishing', 'reports'],
allowBlank: false,
editable: false
}, {
fieldLabel: 'Path',
name: 'path',
xtype: 'boxselect',
delimiter :'/',
store: [],
queryMode: 'local',
forceSelection: false,
createNewOnEnter: true,
createNewOnBlur: true,
}, {
name: 'href',
fieldLabel:'Href'
},, {
name: 'appId',
fieldLabel:'Application Id'
}, {
fieldLabel: 'windowTitle',
name: 'windowTitle'
}
],
setData: function (data) {
data.guid = data.guid || Ext.data.IdGenerator.get('uuid').generate();
this.getForm().setValues(data);
this.data = data;
},
getData: function () {
var data = this.getValues(false, false, false, true);
return Ext.applyIf(data, this.data);
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment