web-dev-qa-db-ja.com

メディアビューテンプレートの入力フィールドから値を取得する方法

2つの入力フィールド(幅と高さ)を持つメディアビューテンプレートを作成しましたが、メディアコントローラビューでそれらに入力された値を取得する方法について疑問に思いました。

PHP:

function add_my_media_view_template(){
?>
    <script type="text/html" id="tmpl-my-media-view">
        <label class="setting width">
            <span><?php _e('Width'); ?></span>
            <input type="text" class="alignment" data-setting="width" />
        </label>
        <label class="setting height">
            <span><?php _e('Height'); ?></span>
            <input type="text" class="alignment" data-setting="height" />
        </label>
    </script>
<?php  }
add_action( 'print_media_templates', 'add_my_media_view_template');

JS:

wp.media.view.MyMediaViewSettings = wp.media.view.Settings.extend({
    className: 'my-media-view',
    template:  wp.media.template('my-media-view'),

    initialize: function() {
        ....
    }

});

wp.media.controller.Custom = wp.media.controller.State.extend({

    initialize: function(){
        ....
    },

    // called when the toolbar button is clicked
    customAction: function(){
        // NEED TO GET THE VALUES OF THE WIDTH AND HEIGHT INPUT FIELDS
    }

});

任意の助けがいただければ幸いです。メディアモーダルはあまり文書化されておらず、それを使って物事を構築するのは困難です。

ありがとう

1
leemon

メディアのモーダルコードを調べた後、私は自分の質問に対する答えを思いついた。

wp.media.controller.Custom = wp.media.controller.State.extend({

    initialize: function(){
        this.props = new Backbone.Model();
    },

    // called when the toolbar button is clicked
    customAction: function( controller ){
        // get the value of a media view template form field using
        // this.props.get('key'), where 'key' is the value of its 
        // 'data-setting' attribute
        console.log('width: ' + this.props.get('width'));
        console.log('height: ' + this.props.get('height'));
    }

});
1
leemon