1

私はJavaScriptコンポーネントの作成を終えており、構成プロパティ、イベント、メソッドなどを説明するためにたくさんの例を提供したいと思っています...

現在、単一のプロパティを文書化する次の HTML があります。

      <div class="conf">
        <div class="opt-name">allowFreeEntries</div>
        <code>boolean</code>
        <div style="clear:both;"></div>
        <div class="opt-desc">Restricts or allows the user to validate typed entries.<br/>Defaults to <em>true</em>
            <a class="opt-ex">View an example</a>
            <span class="hidden">{"allowFreeEntries": false}</span>
        </div>
      </div>

[View an example] リンクをクリックすると、非表示のスパンで指定されたカスタム構成と混合されたデフォルト構成を持つコンポーネントを含む別の div が開きます。

$('.conf .opt-ex').click(function(){
    var raw = $(this).next().html();
    var rawCfg = JSON.parse(raw);
    var exId = 'example-' + $('div[id^="example-"]').length + 1;
    var cfg = $.extend({
        renderTo: $('#' + exId),
        width: 590
    }, rawCfg);
    $('<div/>',{id: exId}).insertBefore(this);
    new MyComponent(cfg);
});

これはすべてうまくいきます...ここでトリッキーな部分が来ます。完全に評価されたコードをコンポーネントのすぐ上に生の HTML として出力したいと思います。ユーザーに表示したいのはこれです:

        <code><pre>
        new MagicSuggest({
            renderTo: $('#example-1'),
            width: 590,
            allowFreeEntries: false                    
        });
        </pre></code>

私はいくつかの実験をしましたが、それらはすべて不器用に見えました。助言がありますか?

どうもありがとう!

4

1 に答える 1

0

私が理解していることから、新しい MagicSuggest 文字列内にオブジェクトの値を出力しようとしています。私の知る限り、これを追加するには文字列追加を使用するだけです。オブジェクト自体を印刷するには、使用できます

   var printObj = typeof JSON != "undefined" ? JSON.stringify : function(obj) {
     var arr = [];
     $.each(obj, function(key, val) {
     var next = key + ": ";
     next += $.isPlainObject(val) ? printObj(val) : val;
     arr.push( next );
     });
     return "{ " +  arr.join(", ") + " }";
   };

参考:http ://api.jquery.com/jQuery.extend/

于 2013-02-14T04:49:01.900 に答える