-1

私が書いたdateTimeクラスの下にある例は、説明よりも簡単になります。

    define([
        'jquery',
        'infrastructure/libWrapper/Backbone',
        'underscore' ],
        function($, Backbone, _ ){

            return Backbone.Model.extend({
                initialize: function(){
                    if(this.get('value') == null){
                        this.set('value', new Date());
                    }
                    else{
                        var parts = this.get('value').match(/\d+/g);
                        this.set('value', new Date(parts[0], parts[1] - 1 , parts[2], parts[3], parts[4], parts[5]));
                    }
                },
                minus : function(dateTime){
                    return this.get('value').getTime() - dateTime.get('value').getTime();
                },
                toISOString : function(){
                    return this.get('value').toISOString();
                },

                defaults:{
                    value: null}
            });
        });

メソッドを実装したい場合: "function plus(duration) {}" は dateTime を返す必要がありますが、どうすればそれを行うことができますか?

4

1 に答える 1

1

モデルを返す前に保存するか、同じ方法で追加することをお勧めしますminus

    define([
    'jquery',
    'infrastructure/libWrapper/Backbone',
    'underscore' ],
    function($, Backbone, _ ){

        var model = Backbone.Model.extend({
            initialize: function(){
                if(this.get('value') == null){
                    this.set('value', new Date());
                }
                else{
                    var parts = this.get('value').match(/\d+/g);
                    this.set('value', new Date(parts[0], parts[1] - 1 , parts[2], parts[3], parts[4], parts[5]));
                }
            },
            minus : function(dateTime){
                return this.get('value').getTime() - dateTime.get('value').getTime();
            },
            // here
            plus : function(dateTime){
                return this.get('value').getTime() + dateTime.get('value').getTime();
            },
            toISOString : function(){
                return this.get('value').toISOString();
            },

            defaults:{
                value: null}
        });
        //or here
        model.plus = function(duration) {   } 

        return model
    });
于 2012-10-22T18:18:05.937 に答える