1

私はしばらくの間開発してきたアプリケーションを常にローカルマシン上に持っています。私は今それをサーバー環境に置いていますが、テンプレートをバックボーンビューに挿入するときのjQueryの.html()関数のタイミングに関連していると思われる問題があります。

コード(重要な部分)について:

application.js

define(["models/mapModel",
        "views/base/mapView"],function(MapModel, MapView){
    var initialize = function(){

    // Set up the application here
    var mapModel, mapView;

    dojo.ready(function(){

        mapModel = new MapModel();

        mapView = new MapView({
            model : mapModel
        });

        $("#appRoot").html(mapView.render().el);

    });
  };

  return {
    initialize: initialize
  };
});

ViewTemplate.html

<div id="map">map goes here</div>

mapView.js

   // 'viewTemplate' is being retrieved with dojo's define() method,
   // this is not the issue as it is working in many other places.

   initialize : function(){
        var selectFeature = lang.hitch(this, this.getFeature);
        topic.subscribe("selectFeature", selectFeature);
    },
    render : function(){
        var tmpl = _.template(viewTemplate);
        this.$el.html(tmpl());
        this.model.on("change:map", this.createLocator, this);

        //Create the map once everything is drawn
        $(document).ready($.proxy(
            function() {
                this.createMap();
            },
        this));

        return this;
    },
    createMap : function(){
        console.log($('#appRoot').length); // returns 1
        console.log($('#map').length);     // returns 0
    }

私の問題は、CreateMap関数の2行で示されています。#appRootはindex.htmlで静的に定義され、#mapはjQueryの.html()関数によってレンダリングに挿入されます。CreateMap()が起動するまでに、#map要素が挿入されていないようです。繰り返しますが、これはローカルホスト以外のマシンからアプリをヒットした場合にのみ発生します。

ありがとうJP

4

1 に答える 1

2

試してみてくださいthis.$('#map').length

$('#map').length#map追加する前にrenderを呼び出しているため、ページにまだ追加されていないため、機能しません。

$("#appRoot").html(mapView.render().el);//ビューをレンダリングしてから、ページに追加します。

以下のコードもそれを修正しますが、this.$とにかく使用する方が良いです。

$("#appRoot").html(mapView.el); // add to page
mapView.render(); // render
于 2013-03-04T19:42:00.843 に答える