1

注: この質問は、古いバージョンの Mozilla x-tag に基づいています。

私のプロジェクトではMozilla x-tagを使用しています。x-master というタグを作成しました。index.html でこれらのタグを 2 つ取得しました。

<div id="page-one" data-role="page">
<x-master id="x-one" data-src="source1"></x-master>
</div>
<div id="page-two" data-role="page">
<x-master id="x-two" data-src="source2"></x-master>
</div>

xtag コンポーネントは次のようになります。

(function(window, document, undefined) {
var jsonurl;

xtag.register('x-master', {
onCreate : function() {
jsonurl = this.getAttribute('data-src');
},
methods : {
getContent : function(){
$.getJSON(jsonurl, function(data){ console.log(jsonurl); };
}
}
})(this, this.document);

私の問題: id x-one のメソッド getContent を呼び出すと、次のように出力されます: source2. どうすればこの動作を防ぐことができますか?

4

4 に答える 4

3

X-Tag の最新バージョンを入手することをお勧めします。標準化プロセスの後期段階を経て仕様が変更されたため、そのバージョンはかなり古いものです。X-Tag 用に現在配置されている API は、最終仕様と互換性があり、IE9 までさかのぼるすべての主要なブラウザーで使用できます: http://x-tags.org/

X-Tag の組み込み機能で最適化されたコードは次のようになります。

xtag.register('x-master', {
    accessors: {
        src: {
            attribute: {},
            set: function(src){
                // Using the attribute <--> accessor auto-linkage feature of 
                // X-Tag, both your created lifecycle event and getContent method
                // become unnecessary. If src is present on creation, or changed
                // at any time, your setter is automatically executed. Simply set
                // the src property/attribute and it will do your AJAX call.
                $.getJSON(src, function(){ ... })
            }
        }
    }
});
于 2013-05-14T15:31:12.837 に答える
2

jsonurlすべての x-master によって共有される変数ではなく、オブジェクトのプロパティを作成します。

var props = function() {
    var jsonurl;

    return { 
        onCreate: function() {
            jsonurl = this.getAttribute('data-src');
        },

        methods: {
            getContent: function() {
                $.getJSON(jsonurl, function(data) {
                    console.log(jsonurl);
                };
            }
        }
    };
};

xtag.register('x-master', props());
于 2012-12-24T15:19:21.840 に答える
2

これを機能させるための解決策は、jsonurl を使用してオブジェクトを作成することです。この正しい解決策を教えてくれたFlorian Margaineに感謝します。

これを行う方法:

(function(window, document, undefined) { 
function props() {
    this.jsonurl; 
}

xtag.register('x-master', {
    onCreate : function() {
       this.props = new props();
       this.props.jsonurl = this.getAttribute('data-src');
},
    methods : {
        getContent : function(){
             $.getJSON(this.props.jsonurl, function(data){ console.log(jsonurl); };
        }
}
})(this, this.document);
于 2013-01-02T10:14:31.770 に答える