2013-05-19 21 views
0

目標: 我想用繼承dojox.mobile.View的獨立模板創建一個dojo小部件。Inherting dojox.mobile.View和使用_TemplatedMixin

//Javascript 
define([ 
"dojo/_base/declare", 
"dijit/_TemplatedMixin", 
"dijit/_WidgetsInTemplateMixin", 
"dojo/text!./templates/myAppview.html", 
"dojox/mobile", "dojox/mobile/deviceTheme", "dojox/mobile/compat", 
"require", 
"dojox/mobile/ListItem", 
"dojox/mobile/Heading"], 
function (declare, _TemplatedMixin, _WidgetsInTemplateMixin, template, mobile, deviceTheme, compat, require) { 
    return declare("widgets.myApp.myAppView", [dojox.mobile.View, _TemplatedMixin, _WidgetsInTemplateMixin], { 
    templateString: template, 
    widgetsInTemplate: true, 
    widgetBase: require.toUrl("widgets/myApp/"), 
    constructor: function (params) { 
     params = params || {}; 
     console.log("constructor" + dojo.toJson(params)); 
    }, 
    startup: function() { 
     this.inherited(arguments); 
    }, 
    postCreate: function() { 
     this.inherited(arguments); 
    } 
}); 
}); 

模板

<div> 
    <div data-dojo-type="dojox/mobile/Heading" data-dojo-props="label: 'Templated View'"> 
    </div> 
</div> 

使用

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
</head> 
<body> 
    <script type="text/javascript" charset="utf-8" src="globals.js"></script> 
    <script type="text/javascript" charset="utf-8" src="//ajax.googleapis.com/ajax/libs/dojo/1.8.3/dojo/dojo.js"></script> 
    <script type="text/javascript"> 
     require([ 
      "dojo/parser", 
      "widgets/myApp/myAppView", 
      "dojo/domReady", 
      "dojox/mobile", "dojox/mobile/deviceTheme", "dojox/mobile/compat" 
     ], 
      function (parser, myApp, ready) { 
       parser.parse(); 
       myApp = new widgets.myApp.myAppView(); 
       myApp.placeAt("kj"); 
       myApp.startup(); 
      }); 

    </script> 

    <div id="kj"> 
    </div> 

    <!--Crash--> 
    <div data-dojo-type="widgets.myApp.myAppView"> 
    </div> 
</body> 
</html> 

如果我編程方式創建控件它啓動,但它不包含任何模板。如果我將它創建爲<div data-dojo-type="widgets.myApp.myAppView">,它會凍結。

如果我只是繼承dojox.mobile.View並在postCreate中以編程方式添加其他小部件,一切正常。但是,爲了保持結構的可維護性,我真的需要能夠繼承dojox.mobile.View並能夠混合模板。

回答

0

您沒有提及您正在使用的Dojo版本。在最近發佈的Dojo 1.9中引入了對View和其他移動小部件進行模板化的支持。所以如果你使用的是舊版本,你需要升級。

在1.9提供有用的資源:

希望這有助於

阿德里安

+0

也做到了,被困在1.8和移動到1.9的伎倆。非常感謝你! –