Using some tutorials I wrote simple widget but it causes error “declare customwidget.TestDijit: mixin #0 is unknown”:
//test.html
<title>Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6.0/dojo/dojo.xd.js" djConfig="parseOnLoad:true">
</script>
<script type="text/javascript" src="Test.js"> </script>
<script type="text/javascript">
dojo.require("customwidget.TestWidget");
</script>
</head>
<body>
<div dojoType='customwidget.TestWidget'/>
</body>
//test.js
dojo.provide('customwidget.TestDijit')
dojo.require('dijit._Widget');
dojo.require('dijit._Templated');
dojo.require('dojo.caсhe');
dojo.declare("customwidget.TestDijit", [dijit._Widget, dijit._Templated],
{
//can't use dojo.caсhe('customwidget.template', 'testdijit.html') I don't know why
templatePath : "",
widgetsInTemplate : true,
lang : 'EN',
postCreate: function(args, frag)
{
this.inherited('postCreate', arguments);
},
clickEvent : function()
{
alert("Button Click event");
}
});
//testdijit.html
<div id="${id}">
<input dojoattachevent="onClick: clickEvent" dojotype="dijit.form.Button" label="Search" />
</div>
I found out that if I place all widget’s code in the dojo.ready funciton, then it will work(thank to this article). Of course, I don’t want to locate all my js code in the ready function. In the mentioned article’s code sample authors wrote // Future tutorials will explain how to properly separate this out into its own file.
Do you know how to solve this problem?
PS. Do you also know why I can’t use dojo.cashe in the this js code?
UPD: Problems with loading cross-domain resources.
here is similar discussion but I couldn understand how to solve the problem. I can store dojo localy but in this case it couldn’t find my TestWidget.js – I don’t how I can specify the path to my scipts. If I do that using baseUrl it will say “Could not load ‘dojo._base.lang’; last tried ‘./_base/lang.js'”.
test/test.html
<!DOCTYPE html>
<head>
<title>Test</title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/resources/dojo.css">
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dijit/themes/claro/claro.css">
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojox/grid/resources/Grid.css">
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojox/grid/resources/claroGrid.css">
<link rel="stylesheet" href="css/layout.css">
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6.0/dojo/dojo.xd.js"
djConfig="parseOnLoad:true, baseUrl: './'">
</script>
<script type="text/javascript">
dojo.require("customwidget.TestWidget");
</script>
</head>
<body class="claro">
<div dojoType='js.Test'>
</div>
</body>
test/customwidget/TestWidget.js
dojo.provide('customwidget.TestWidget')
dojo.require('dijit._Widget');
dojo.require('dijit._Templated');
dojo.require("dijit.form.Button");
//dojo.require('dojo.cache');
dojo.declare("customwidget.TestWidget", [dijit._Widget, dijit._Templated],
{
templatePath : "",//dojo.cache('customwidget.template', 'testdijit.html'),
widgetsInTemplate : true,
lang : 'EN',
postCreate: function(args, frag)
{
this.inherited('postCreate', arguments);
},
clickEvent : function()
{
alert("Button Click event");
}
});
There are a few things first (but I’m guessing these are mostly typos you made when posting here).
dojo.casheshould bedojo.cachecustomwidget.TestWidgetwithcustomwidget.TestDijitdijit.form.Buttonin your widget’s template, you have to require itNow onto the more important matters. When you use a <script> tag to include the js file, Dojo doesn’t take care of loading requirements before doing anything else. That’s why you get mixin unknown, because in the call to
dojo.declare, the classdijit._Templatedis not yet loaded.However, if you use dojo.require to load your widgets/modules, dojo makes sure the requirements (all the dojo.require statements) in Test.js are done loading before trying to use them. So, remove your <script>-tag for Test.js.
Now we need to tell
dojo.requirewhere your files can be found. You are using dojo from a CDN (googleapis), so by default, Dojo will actually try to loadThat’s not right at all! Rename your Test.js to TestWidget.js and put it in a folder called customwidget. This is the Dojo convention for module paths. If your widget was called
customwidget.coolwidgets.TestWidget, it should be in customwidget/coolwidgets/TestWidget.js.For now, put this folder next to your HTML file. Then add the following to your
djConfig:This tells
dojo.requireto start looking for widgets in the same folder as your HTML file, and not on the server where you are loading Dojo itself from. Since we put the customwidget folder next to out HTML file, that should work fine.In your
dojo.cachecall you are usingcustomwidget.templateand testdijit.html. That means your testdijit.html file has to be placed in customwidget/template/Edit: Here’s a setup that works correctly on my machine.
Folder structure:
test.html:
TestWidget.js