在ractive js中使用下划线进行排序

using underscore in ractive js to sort

我正在使用 ractive js 作为模板。

下面是我的模板

<form data-type="Contractor" id="Contractor">
    <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="box no-shadow box-fullborder no-radius form-section">
            <div class="box-header gray-bg">
                <h5>Select Postal Code</h5>
            </div>
            <div class="box-body gray-bg">

              <div class="form-group">
                <label>Postal Code</label>
                <select name="{{postal}}"  class="form-control select2 postal"  data-placeholder="Select your postal code" >


                </select>
              </div><!-- /.form-group -->


            </div>
        </div>
        {{#if isPostalSelected}}
        <div class="box no-shadow box-fullborder no-radius form-section">
            <div class="box-header gray-bg">
                <h5>Select Services</h5>
            </div>
            <div class="box-body gray-bg">
                {{#each services}}

                    <div class="form-group col-md-6 col-sm-12 col-xs-12">
                        <label>
                            <input type="checkbox" name="{{ selectedServices }}" value="{{Id}}" class="minimal flat-green"/>
                            <span>{{Title}}</span>
                        </label>
                        {{#if IsMultiple}}
                        <label>
                            <input class="timeRange" type="text" name="range_5" value="">
                        </label>
                        {{/if}}
                    </div>

                {{/each}}


            </div>

        </div>
        {{/if}}
    </div>
    <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="box no-shadow box-fullborder no-radius form-section">
            <div class="box-body gray-bg">
                <div class="pull-right">
                  <button type="submit" class="btn btn-sm btn-flat" data-action="savePostal" data-form="Contractor" data-id="Contractor">
                    &nbsp;&nbsp;&nbsp;Next&nbsp;&nbsp;&nbsp;
                  </button>

                </div>
            </div>
        </div>
    </div>
</form>

下面是我在模板中传递的 Json

{
  "selectedServices": [],
  "postal": "",
  "services": [
    {
      "BaseTime": 0.5,
      "Correction": null,
      "Id": "a0M23000000IoabEAC",
      "IsMultiple": false,
      "serviceOrder": 3,
      "Title": "Fridge"
    },
    {
      "BaseTime": 0.5,
      "Correction": null,
      "Id": "a0M23000000IoagEAC",
      "IsMultiple": false,
      "serviceOrder": 4,
      "Title": "Oven"
    },
    {
      "BaseTime": 0.5,
      "Correction": 0.5,
      "Id": "a0M23000000IoalEAC",
      "IsMultiple": false,
      "serviceOrder": 5,
      "Title": "Walls"
    },
    {
      "BaseTime": 0.333,
      "Correction": null,
      "Id": "a0M23000000IoaMEAS",
      "IsMultiple": true,
      "serviceOrder": 0,
      "Title": "Bedrooms"
    },
    {
      "BaseTime": 0.5,
      "Correction": 0.5,
      "Id": "a0M23000000IoaqEAC",
      "IsMultiple": false,
      "serviceOrder": 6,
      "Title": "Windows"
    },
    {
      "BaseTime": 0.5,
      "Correction": null,
      "Id": "a0M23000000IoaREAS",
      "IsMultiple": true,
      "serviceOrder": 1,
      "Title": "Bathrooms"
    },
    {
      "BaseTime": 0.333,
      "Correction": 0.5,
      "Id": "a0M23000000IoavEAC",
      "IsMultiple": false,
      "serviceOrder": 7,
      "Title": "Blinds"
    },
    {
      "BaseTime": 0.5,
      "Correction": 0.2,
      "Id": "a0M23000000IoaWEAS",
      "IsMultiple": false,
      "serviceOrder": 2,
      "Title": "Cabinets"
    }
  ],
  "isPostalSelected": true
}

我想按 serviceOrder 对服务列进行排序。

我尝试使用 ractive 给出的示例但失败了:(

下面是我的 javascript render ractive 模板代码

 cerateBookingForm = function(){
                        var d = $.Deferred();
                        bookingForm = new Ractive({
                            el: '#bookingForm',
                            template: '#FF_Booking_Form',
                            data: $.Circle.Varriables.BookingForm,
                            testMethod : function(){
                                console.log('test');
                                console.log(data);
                            },
                            onrender : function(data){
                                console.log('rendered');
                                d.resolve(data);
                            },
                            onupdate : function(){
                                $.Circle.App.Ui.checkbox();
                                $.Circle.App.Ui.timeRange();
                                }
                        });
                        return d;
                    }

cerateBookingForm();

谁能帮我实现这个目标?

您可以在数据对象中包含下划线,或者与其余数据内联:

data: {
    _: _,
    selectedServices: _,
    ...
}

或者如果您想在所有组件中使用或将数据封装在 $.Circle.Varriables.BookingForm 中,您也可以加上 prototype:

Ractive.protototype.data = { _: _ }

那么你可以直接在你的模板中使用它:

{{#each _.sortBy(services, 'serviceOrder')}}
    <!-- block content -->
{{/each}}

您也可以使用引用使其动态化:

{{#each _.sortBy(services, sortBy)}}

var r = new Ractive({
    el: document.body,
    template: '#template',
    data: {
        "sortBy": 'serviceOrder',
  "_": _,
        "selectedServices": [],
        "postal": "",
        "services": [{
            "BaseTime": 0.5,
            "Correction": null,
            "Id": "a0M23000000IoabEAC",
            "IsMultiple": false,
            "serviceOrder": 3,
            "Title": "Fridge"
        }, {
            "BaseTime": 0.5,
            "Correction": null,
            "Id": "a0M23000000IoagEAC",
            "IsMultiple": false,
            "serviceOrder": 4,
            "Title": "Oven"
        }, {
            "BaseTime": 0.5,
            "Correction": 0.5,
            "Id": "a0M23000000IoalEAC",
            "IsMultiple": false,
            "serviceOrder": 5,
            "Title": "Walls"
        }, {
            "BaseTime": 0.333,
            "Correction": null,
            "Id": "a0M23000000IoaMEAS",
            "IsMultiple": true,
            "serviceOrder": 0,
            "Title": "Bedrooms"
        }, {
            "BaseTime": 0.5,
            "Correction": 0.5,
            "Id": "a0M23000000IoaqEAC",
            "IsMultiple": false,
            "serviceOrder": 6,
            "Title": "Windows"
        }, {
            "BaseTime": 0.5,
            "Correction": null,
            "Id": "a0M23000000IoaREAS",
            "IsMultiple": true,
            "serviceOrder": 1,
            "Title": "Bathrooms"
        }, {
            "BaseTime": 0.333,
            "Correction": 0.5,
            "Id": "a0M23000000IoavEAC",
            "IsMultiple": false,
            "serviceOrder": 7,
            "Title": "Blinds"
        }, {
            "BaseTime": 0.5,
            "Correction": 0.2,
            "Id": "a0M23000000IoaWEAS",
            "IsMultiple": false,
            "serviceOrder": 2,
            "Title": "Cabinets"
        }]
    }
});
<script src='https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js'></script>
<script src='http://cdn.ractivejs.org/edge/ractive.min.js'></script>
<script id='template' type='text/ractive'>
 {{#each services.0:key}}
  <div><label><input type='radio' name='{{~/sortBy}}' value='{{key}}'> {{key}}</label></div>
 {{/each}}
    {{#each _.sortBy(services, sortBy)}}

    <div class="form-group col-md-6 col-sm-12 col-xs-12">
        <label>
            <input type="checkbox" name="{{ ~/selectedServices }}" value="{{Id}}" class="minimal flat-green" />
            <span>{{Title}}</span>
        </label>
        {{#if IsMultiple}}
        <label>
            <input class="timeRange" type="text" name="range_5" value="">
        </label>
        {{/if}}
    </div>

    {{/each}}

</script>