dojo Cannot set property 'id' of undefined - dojo

I am making a dynamic GridContainer. However, an error occurred. console.log output a correct value is outputted. However, the execution error. i don't understand this situation.
my code.
define([ "dojo/parser", "dojo/dom", "dojo/dom-style",
"dijit/registry","dojo/on", "dojo/request", "dojo/dom-construct","dojo/json",
"dojo/_base/array", "dijit/Dialog","dijit/focus" ,"dijit/form/Button","dijit/form/Select","dijit/form/TextBox",
"dijit/form/CheckBox", "dojo/query","dojox/layout/GridContainer","dojox/widget/Portlet","dijit/layout/BorderContainer",
"dijit/layout/TabContainer", "dijit/layout/ContentPane"], function(parser, dom, domStyle,
registry,on, request, domConstruct,json, arrayUtil, Dialog,focus,Button, CheckBox,query,GridContainer,Portlet) {
function addTab(formCount,num){//make gridContainer ajax
var xhrArgs = {
url: '/checkData',
handleAs: "json",
content:{
seq:num
},
load:function(data){
var cont='';
++gridCounter;
cont+='<div dojoType="dojox.layout.GridContainer" class="test" doLayout="true" id="gc'+gridCounter+'" region="center" hasResizableColumns="false" opacity="0.3" nbZones="1" allowAutoScroll="false" withHandles="true" dragHandleClass="dijitTitlePaneTitle" minChildWidth="200" minColWidth="10" style="height:50%;">';
..........
}
}
function addGridContainer(id){
alert('vidgetAddgridNo:'+id);
var result='';
var xhrArgs = {
url: '/checkVidget',
handleAs: "json",
content:{
id:id
},
load: function(data){
++addPorNum;
result+=data;
var portletContent2 = [
domConstruct.create('div', {innerHTML: result})
];
var portlet2 = Portlet({
id: 'dynPortlet'+addPorNum,
closable: false,
title: ''+result,
content: portletContent2
});
makeGrid(portlet2);
}
}//end xhrArgs
var deferred = dojo.xhrGet(xhrArgs);
function makeGrid(por){
console.log('makeGrid in');
var selectedTab=registry.byId('tabContainer').get('selectedChildWidget');
var tabs=registry.byId("tabContainer");
var cPane=tabs.get("selectedChildWidget");
var grid=cPane.getChildren()[0];
var id=grid.id;
alert(registry.byId(id));=>[Widget dojox.layout.GridContainer,gc1]
registry.byId(id).addChild(por,0,0);=>Error:Cannot set property 'id' of undefined
}//end makeGrid()
}
plz help me..T.T

In your define() module list you defined the following modules:
"dijit/form/Button", "dijit/form/Select", "dijit/form/TextBox",
"dijit/form/CheckBox"
However, in your callback you only have:
Button, CheckBox
So, you're missing the Select and TextBox module here, meaning that the parameter CheckBox actually contains the module dijit/form/Select. Which means every module is shifted and none of them contain the actual value, try fixing that first.

Related

Dojo AMD module changes reference of "this"

I'm really confused as to some behavior I'm seeing in trying to learn the AMD style of Dojo. When I instantiate my module/object, "this" refers to the object in my constructor. I make a call to an internal function, and "this" inside that internal function refers to the Window object. So when I get to this.attachMapEventHandlers I get a "Object [object global] has no method 'attachMapEventHandlers'" error. What am I doing wrong? UPDATE: I found lang.hitch, which seems to indicate that the async nature is what's tripping me up, but I'm confused on how to implement a solution.
my script inside index.html:
require(["javascript/layout", "dijit/layout/ContentPane", "dijit/layout/BorderContainer", "dijit/layout/AccordionContainer",
"dojo/dom", "dojo/dom-attr", "dijit/Toolbar", "dijit/form/Button", "dijit/Dialog","dijit/ProgressBar", "dojo/domReady!"],
function (layout, dom, domAttr) {
mapControl = new layout();
layout.js:
define(["dojo/_base/declare"], function(declare) {
return declare(null, {
action:"pan",
activeMeasureTool:"",
aerialLayer:"",
legendLayers:"",
loadedServices:"",
popup:"",
resizeId:0,
constructor: function() {
this.init();
},
init: function() {
require(["esri/map", "esri/config", "esri/SpatialReference", "esri/geometry/Extent"],
function(Map, config, SpatialReference, Extent) {
//custom map requires a proxy to function properly.
esri.config.defaults.io.proxyUrl = "../sdc_devdata/proxy.php";
var spatRef = new SpatialReference(2276);
var startExtent = new Extent(2481416.32087491, 6963246.42495962, 2501196.36936991, 6980267.92469462, spatRef);
var appFullExtent = new Extent(2396699.46935379, 6872369.60195443, 2607745.94404633, 7107335.22319087, spatRef);
map = new Map("map", {extent: startExtent, isZoomSlider:true, logo:false, sliderStyle:"large"});
this.attachMapEventHandlers();
this.createLayers();
this.handleLayerVisibilityChange();
});
},
There are a couple of things that you could do to resolve this.
Firstly, you could add your required dependency to the define array so you don't need to do an asynchronous require within the constructor of the class.
That would look like:
define(["dojo/_base/declare", "esri/map", "esri/config", "esri/SpatialReference", "esri/geometry/Extent"], function (declare, Map, config, SpatialReference, Extent) {
return declare(null, {
action: "pan",
activeMeasureTool: "",
aerialLayer: "",
legendLayers: "",
loadedServices: "",
popup: "",
resizeId: 0,
constructor: function () {
this.init();
},
init: function () {
//custom map requires a proxy to function properly.
esri.config.defaults.io.proxyUrl = "../sdc_devdata/proxy.php";
var spatRef = new SpatialReference(2276);
var startExtent = new Extent(2481416.32087491, 6963246.42495962, 2501196.36936991, 6980267.92469462, spatRef);
var appFullExtent = new Extent(2396699.46935379, 6872369.60195443, 2607745.94404633, 7107335.22319087, spatRef);
map = new Map("map", {
extent: startExtent,
isZoomSlider: true,
logo: false,
sliderStyle: "large"
});
this.attachMapEventHandlers();
this.createLayers();
this.handleLayerVisibilityChange();
}
});
});
OR you could save the current scope of this to something in the closure when doing the require
init: function () {
var that = this;
require(["esri/map", "esri/config", "esri/SpatialReference", "esri/geometry/Extent"],
function (Map, config, SpatialReference, Extent) {
//custom map requires a proxy to function properly.
esri.config.defaults.io.proxyUrl = "../sdc_devdata/proxy.php";
var spatRef = new SpatialReference(2276);
var startExtent = new Extent(2481416.32087491, 6963246.42495962, 2501196.36936991, 6980267.92469462, spatRef);
var appFullExtent = new Extent(2396699.46935379, 6872369.60195443, 2607745.94404633, 7107335.22319087, spatRef);
map = new Map("map", {
extent: startExtent,
isZoomSlider: true,
logo: false,
sliderStyle: "large"
});
that.attachMapEventHandlers();
that.createLayers();
that.handleLayerVisibilityChange();
});
},
EDIT: your third option would be using lang.hitch, which lets you specify the scope of this in the the callback function. To use it, you would add dojo/_base/lang to you define() dependency list, and wrap the require callback in lang.hitch(this, function(){});
define(["dojo/_base/declare", "dojo/_base/lang"], function (declare, lang) {
return declare(null, {
//...
init: function () {
require(["esri/map", "esri/config", "esri/SpatialReference", "esri/geometry/Extent"],
lang.hitch(this, function (Map, config, SpatialReference, Extent) {
//this now refers to the instance of the class
}));
}
});
});
I would strongly suggest going with the first option, since it is consistent with the entire use of AMD (declaring what dependencies a module needs before it executes, rather than loading it on the fly).
You can put 'this' in another variable, like _this, and use _this in your internal function,like
init: function() {
var _this= this;
require(["esri/map", "esri/config", "esri/SpatialReference", "esri/geometry/Extent"],
function(Map, config, SpatialReference, Extent) {
...
_this.attachMapEventHandlers();
_this.createLayers();
_this.handleLayerVisibilityChange();

Widgets inside Dojo dgrid OnDemandList

I'm trying to do something similar to this question but using an OnDemandList instead of an OnDemandGrid.
Here is what I have so far
define([
"dojo/_base/declare",
"dijit/_WidgetBase",
"dijit/_TemplatedMixin",
"dijit/_WidgetsInTemplateMixin",
"dgrid/OnDemandList",
"widget/CategoryItem",
"dojo/dom-construct",
"dojo/text!./templates/category-list.html"
], function(declare, _Widget, _TemplatedMixin, _WidgetsInTemplateMixin, OnDemandList, CategoryItem, domConstruct, template) {
var CatList = declare([OnDemandList]);
return declare([_Widget, _TemplatedMixin, _WidgetsInTemplateMixin], {
templateString: template,
baseClass: "category-list",
postCreate: function() {
this.inherited(arguments);
// Create OnDemandList, place in div defined in template.
var cat1 = this.cat1 = new CatList({
store: this.store,
renderRow: this.renderItem
}, this.categoriesLevel0);
},
renderItem: function(item) {
return new CategoryItem({
title: item.title
});
}
});
});
The problem is my renderItems function needs to somehow return a dom containing my custom widget. As it is now I get this error Error on domReady callback: Error: NotFoundError: DOM Exception 8
Yeah it definitely needs to return a dom node from renderRow. Assuming you're using _WidgetBase for CategoryItem it should work like:
renderItem: function(item) {
var category = new CategoryItem({
title: item.title
});
return category.domNode;
}
The example here: https://github.com/SitePen/dgrid/wiki/OnDemandList-and-OnDemandGrid does pretty much the same thing, except it uses put-selector, which is just constructing a div, attaching the widget to it and returning the new div.

dojox.mobile: Custom ListItem widget: "... is not a constructor"

I want to create a custom ListItem widget for my dojox.mobile app. It works if i use it in my HTML Code, but it throws a TypeError if i try to use it programmatically.
This is the JS-Code for my custom ListItem:
define([
"dojo/_base/declare",
"dojo/dom-construct",
"dojox/mobile/ListItem"], function(declare, domConstruct, ListItem){
var LabeledInputListItem = declare("myapp.LabeledInputListItem", ListItem, {
labelText: "",
placeholder: "",
value: "",
_setItemLabelAttr: function(val) {
this.labelText = val;
this.qDescSpan.innerHTML = val;
},
_setPlaceholderAttr: function(val) {
this.placeholder = val;
},
_setValueAttr: function(val) {
this.value = val;
},
startup: function(){
if(this._started){ return; }
},
constructor: function(params) {
this.placeholder = params.placeholder;
this.labelText = params.labelText;
this.valu = params.value;
},
buildRendering: function(){
this.inherited(arguments);
this.qDescDiv = domConstruct.create("div", {className: "tableItemDescription", id: "asd"}, this.labelNode, "before");
this.qDescSpan = domConstruct.create("span", null, this.qDescDiv, "first");
this.qInputDiv = domConstruct.create("div", {className: "tableItemInput"}, this.qDescDiv, "after");
this.qInputText = domConstruct.create("input", {className: "mblTextBox sessionTextbox", placeholder: this.placeholder, value: this.value}, this.qInputDiv, "first");
console.log(this.labelText, this.placeholder, this.value);
},
});
return LabeledInputListItem; });
I can use this custom ListItem in my html Code like this:
<li data-dojo-type="myapp/LabeledInputListItem" data-dojo-props="itemLabel: 'asdasd', placeholder: 'placeholder', value: 'value'"></li>
However, if I try to create my custom ListItem programmatically it results in the following error:
TypeError: myapp.LabeledInputListItem is not a constructor
var childWidget = new myapp.LabeledInputListItem({placeholder: "placeholder"});
Does anybody know what i'm missing?
Thanks in advance for your help!
The only (obvious) reason I can think of would be that you did not require the module?
By the way Dojo is now moving towards a "no globals" approach, so it may be better to not give an explicit id to your class, and use the value of the AMD module instead of the global myapp.LabeledInputListItem.

How do I reload a FilteringSelect in Dojo 1.7?

I have created a FilteringSelect with the following code:
var type = $('fMatlTypeId').value;
var eSelect, dataStore;
require([
"dijit/form/FilteringSelect",
"dojo/store/Memory",
"dojo/data/ObjectStore",
"dojo/_base/xhr",
"dojo/parser",
"dojo/domReady!"
], function(FilteringSelect, Memory, ObjectStore, xhr){
xhr.get({
url: 'adminservices/admin/materialType/assignedFacilities/' + type + '/',
handleAs: "json"
}).then(function(data){
dataStore = new ObjectStore({ objectStore:new Memory({ data: data.items }) });
eSelect = new FilteringSelect({
id: 'fMatlTypeFacilities',
store: dataStore,
searchAttr: 'nameStatus',
queryExpr: '*${0}*',
ignoreCase: true,
autoComplete: false,
style: 'width:200px',
required: true
}, document.createElement('div'));
// Append to the div
$('FS_MatlTypeFacilities').innerHTML = '';
dojo.byId("FS_MatlTypeFacilities").appendChild(eSelect.domNode);
eSelect.startup();
eSelect.setValue(dataStore.objectStore.data[0].id);
});
});
Now if the data changes in the backend how do I reload it?
I have tried the following code and when I debug it in Firebug the store gets updated but not the FilteringSelect.
var eSelect, dataStore;
require([
"dijit/form/FilteringSelect",
"dojo/store/Memory",
"dojo/data/ObjectStore",
"dojo/_base/xhr",
"dojo/parser",
"dojo/domReady!"
], function(FilteringSelect, Memory, ObjectStore, xhr){
xhr.get({
url: 'adminservices/admin/materialType/assignedFacilities/' + type + '/',
handleAs: "json"
}).then(function(data){
dataStore = new ObjectStore({ objectStore:new Memory({ data: data.items }) });
dataStore.fetch();
eSelect = dijit.byId('fMatlTypeFacilities');
eSelect.store.close();
eSelect.store = dataStore;
eSelect.startup();
});
});
Any suggestions?
The only thing I have found to work is simply destroy the widget each time and let it rebuild. So I added the following code before the above create code. But there has got to be a way to simply reload it.
if (dijit.byId('fMatlTypeFacilities')) dijit.byId('fMatlTypeFacilities').destroy();
Try changing:
eSelect.store = dataStore;
to
eSelect.set('store', dataStore);
In general, you should access a dijit's properties through myDijit.set('');. See here for more details

dojo nested Custom Widget undefined not a function

I've a Widget called stat.widget.Weekly that is a _Container and it require's stat.widget.Daily as Daily But Whenever I use new Daily() I get
Uncaught TypeError: undefined is not a function
My Code goes like this
require([
"dojo/_base/declare", "dojo/parser", ..., "stat/widget/Daily", "dijit/_Container"
], function(declare, ... , _WidgetBase, _TemplatedMixin, Daily, _Container){
declare("stat.widget.Weekly", [_WidgetBase, _TemplatedMixin, _Container], {
....
update: function(){
new Daily();//< Fires Error
},
postCreate: function(){
var self = this;
setTimeout(function(){
self.update();
}, 500);
}
});
});
But this stat/widget/Daily can be be instantiated in console with new
If your stat.widget.Weekly is placed in a file under stat-Modulepath/widget/Daily.js, this syntax would be more smooth to process in the classloader:
define([ // using define instead of require
"dojo/_base/declare", "dojo/parser", ..., "stat/widget/Daily", "dijit/_Container"
], function(declare, ... , _WidgetBase, _TemplatedMixin, Daily, _Container){
var myPrivates = declare("stat.widget._WeeklyResource", [], {
...
});
var myDefinition = declare("stat.widget.Weekly", [_WidgetBase, _TemplatedMixin, _Container], {
...
});
// returning the definition
return myDefinition;
});