jjzjj

javascript - dijit.byId 不工作(不是函数?)

coder 2025-02-23 原文

这是我的简单 dojo 示例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.9.2/dijit/themes/dijit.css">
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.9.2/dijit/themes/claro/claro.css">
<title>ShowMovies </title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" data-dojo-config="isDebug: false, async: true, parseOnLoad: true" src="http://ajax.googleapis.com/ajax/libs/dojo/1.9.2/dojo.js"></script>
<script type="text/javascript">
    require(
    [ "dojo", "dojo/parser", "dijit/layout/BorderContainer",
            "dijit/layout/ContentPane", "dojox/grid/DataGrid",
            "dojo/data/ItemFileReadStore" ],
    function(dojo) {
        dojo.ready(function() {
            dojo.xhrGet( {
                url : "MovieList.json",
                handleAs : "json",
                load : function(response, ioArgs) {
                    var newData = {
                        identifier: "title",
                        items: response.result
                    };
                    var dataStore = new dojo.data.ItemFileReadStore({data: newData, id:"dataStoreId"});
                    var grid = dijit.byId("gridId");
                    grid.setStore(dataStore);
                },
                error : function(response, ioArgs) {
                    alert(response);
                }
            });
        });
    });
</script>
<link rel="stylesheet" type="text/css" title="Style" href="http://ajax.googleapis.com/ajax/libs/dojo/1.9.2/dojox/grid/resources/Grid.css">
<link rel="stylesheet" type="text/css" title="Style" href="http://ajax.googleapis.com/ajax/libs/dojo/1.9.2/dojox/grid/resources/claroGrid.css">
</head>
<body class="claro">
    <div id="BorderContainer" style="height: 100%; width: 100%"
        data-dojo-type="dijit.layout.BorderContainer"
        data-dojo-props="design:'headline'">
        <div data-dojo-type="dijit.layout.ContentPane"
            data-dojo-props="region:'top'" style="text-align: center">My Movie Web Application!</div>
        <div data-dojo-type="dijit.layout.ContentPane"
            data-dojo-props="region:'center'">
            <table id="gridId" autowidth="true"
                data-dojo-type="dojox.grid.DataGrid"
                data-dojo-props="rowSelector:'20px'">
                <thead>
                    <tr>
                        <th field="title">Title</th>
                        <th field="director">Director</th>
                        <th field="actor">Actor</th>
                        <th field="description">Description</th>
                    </tr>
                </thead>
            </table>
        </div>
        <div data-dojo-type="dijit.layout.ContentPane"
            data-dojo-props="region:'right'"></div>
    </div>
</body>
</html>

为什么我会收到 xhrGet 请求的错误消息?响应是:dijit.byid 不是函数

谢谢

最佳答案

快速回答是:当您的 Dojo 配置中有 async:true 时,您的 Javascript 代码中可能不应该有任何以 dojo.dijit。

在过去,在 Dojo 1.5 及更早版本中,Dojo 及其模块的工作方式与今天略有不同。

那时,您可以包含 dojo.js 并立即获得一系列方便的函数,例如 dojo.createdojo.connect dojo.xhrGetdijit.byId 等等。如果你想包含一些额外的模块或小部件,你可以使用 dojo.require 然后用 dojo.some.thingdijit.other 引用模块.thing.

在较新版本的 Dojo 中,当您在页面中包含 dojo.js 时,您实际上只会获得两个函数:requiredefine。您使用这些函数来“导入”您需要的一切。即使对于像 dojo.create 这样小的东西,您也必须导入一个模块。

一开始,您可能会觉得这很不方便。如果您对 Dojo 采取这个方向的原因及其好处感兴趣,可以查看此 slideshare .

回到你的代码。你有 async:true 和很多 dojo. 和一个 dijit. 语句。这里有 3 种方法可以解决它:

  1. async:true 更改为 false。这使得 Dojo 以“旧”风格处理代码,即您的 dojo.dijit. 应该仍然有效。

  2. 保留 async:true,但导入特殊的 dijit/dijit 模块,这使得旧的 dijit. 功能可用。所以你的第一行是这样的:

    require(["dojo","dijit/dijit",....], function(dojo, dijit, ...) {

    (“dojo”模块也是一个允许“旧”风格的特殊模块。)

  3. 将您的代码全部重写为新的 AMD 风格。这意味着对于每个 dojo.dijit.,您需要找出需要导入的模块。如果您已经有很多 Dojo 代码,那么这将是一项艰巨的工作。您问题中的代码看起来像这样:http://fiddle.jshell.net/8DETs/

你可能在想:为每一件小事加载一个文件/模块一定很慢!你是对的。这个想法是,当您在本地开发时,它会足够快,并且当您部署您的 Web 应用程序时,您将使用 Dojo 的构建工具在很少的文件中创建一个精简包。这是 Dojo 的绝对优势之一,您可以在此处阅读更多相关信息:http://dojotoolkit.org/documentation/tutorials/1.9/build/

关于javascript - dijit.byId 不工作(不是函数?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21330812/

有关javascript - dijit.byId 不工作(不是函数?)的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  4. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  5. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  6. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  7. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  8. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  9. ruby - JetBrains RubyMine 3.2.4 调试器不工作 - 2

    使用Ruby1.9.2运行IDE提示说需要gemruby​​-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall

  10. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

随机推荐