Τα μείγματα με γραφομηχανή

ψήφοι
6

Παίζω γύρω με γραφομηχανή, και έχω ένα ζευγάρι λειτουργική τα μείγματα , Eventableκαι Settableότι θα ήθελα να Μίχίη σε μια Modelκατηγορία (προσποιούνται ότι είναι κάτι σαν ένα μοντέλο Backbone.js):

function asSettable() {
  this.get = function(key: string) {
    return this[key];
  };
  this.set = function(key: string, value) {
    this[key] = value;
    return this;
  };
}

function asEventable() {
  this.on = function(name: string, callback) {
    this._events = this._events || {};
    this._events[name] = callback;
  };
  this.trigger = function(name: string) {
    this._events[name].call(this);
  }
}

class Model {
  constructor (properties = {}) {
  };
}

asSettable.call(Model.prototype);
asEventable.call(Model.prototype);

Ο παραπάνω κώδικας λειτουργεί καλά, αλλά δεν θα συγκεντρώνουν αν προσπάθησα να χρησιμοποιήσετε μία από τις μικτού στις μεθόδους όπως (new Model()).set('foo', 'bar').

Μπορώ να επιλύσετε αυτό το πρόβλημα

  1. προσθέτοντας interfaceδηλώσεις για τις τα μείγματα
  2. δηλώνοντας κινήσεις get/ set/ on/ triggerμεθόδους με τη Modelδήλωση

Είναι ένα καθαρό τρόπο γύρω από τις δηλώσεις εικονική εκεί;

Δημοσιεύθηκε 04/10/2012 στις 04:20
πηγή χρήστη
Σε άλλες γλώσσες...                            


3 απαντήσεις

ψήφοι
12

Εδώ είναι ένας τρόπος για να προσεγγίσει τα μείγματα που χρησιμοποιούν interfacesκαι static create()μέθοδο. Διασυνδέσεις υποστηρίζει πολλαπλή κληρονομικότητα, έτσι ώστε να αποτρέπει από το να έχουν να επαναπροσδιορίσει την interfacesΓια τα μείγματα και η static create()μέθοδος φροντίζει να σας δώσω πίσω μια παρουσία του Model()ως IModel(το <any>καστ είναι απαραίτητη για να καταστείλει μια προειδοποίηση compiler.) Θα πρέπει να αντιγράψουν όλα ορισμούς μέλος για Modelγια IModelτις οποίες είναι χάλια, αλλά φαίνεται ότι το καθαρότερο τρόπο για να πετύχετε αυτό που θέλετε στην τρέχουσα έκδοση της γραφομηχανή.

edit: Έχω εντοπίσει ένα ελαφρώς απλούστερη προσέγγιση για την υποστήριξη τα μείγματα και έχουν ακόμη δημιουργήσει ένα βοηθητικό τάξη για τον ορισμό τους. Λεπτομέρειες μπορείτε να βρείτε εδώ .

function asSettable() {
  this.get = function(key: string) {
    return this[key];
  };
  this.set = function(key: string, value) {
    this[key] = value;
    return this;
  };
}

function asEventable() {
  this.on = function(name: string, callback) {
    this._events = this._events || {};
    this._events[name] = callback;
  };
  this.trigger = function(name: string) {
    this._events[name].call(this);
  }
}

class Model {
  constructor (properties = {}) {
  };

  static create(): IModel {
      return <any>new Model();
  }
}

asSettable.call(Model.prototype);
asEventable.call(Model.prototype);

interface ISettable {
    get(key: string);
    set(key: string, value);
}

interface IEvents {
    on(name: string, callback);
    trigger(name: string);
}

interface IModel extends ISettable, IEvents {
}


var x = Model.create();
x.set('foo', 'bar');
Απαντήθηκε 04/10/2012 στις 06:46
πηγή χρήστη

ψήφοι
3

Η καθαρότερη τρόπος για να το κάνουμε, Παρ'ότι ακόμα απαιτεί δηλώσεις διπλού τύπου, είναι να καθορίσει το Μίχίη ως μονάδα:

module Mixin {
    export function on(test) {
        alert(test);
    }
};

class TestMixin implements Mixin {
    on: (test) => void;
};


var mixed = _.extend(new TestMixin(), Mixin); // Or manually copy properties
mixed.on("hi");

Μια εναλλακτική λύση στη χρήση διεπαφές είναι να χαράξει με κατηγορίες (Αν και λόγω της πολλαπλής κληρονομικότητας, θα πρέπει να δημιουργήσετε μια κοινή διεπαφή για τα μείγματα):

var _:any;
var __mixes_in = _.extend; // Lookup underscore.js' extend-metod. Simply copies properties from a to b

class asSettable {
    getx(key:string) { // renamed because of token-clash in asEventAndSettable
        return this[key];
    }
    setx(key:string, value) {
        this[key] = value;
        return this;
    }
}

class asEventable {
    _events: any;
    on(name:string, callback) {
        this._events = this._events || {};
        this._events[name] = callback;
    }
    trigger(name:string) {
        this._events[name].call(this);
  }
}

class asEventAndSettable {
   // Substitute these for real type definitions
   on:any;
   trigger:any;
   getx: any;
   setx: any;
}

class Model extends asEventAndSettable {
    /// ...
}

var m = __mixes_in(new Model(), asEventable, asSettable);

// m now has all methods mixed in.

Όπως σχολίασε την απάντησή του Steven, τα μείγματα πρέπει πραγματικά να είναι ένα χαρακτηριστικό γραφομηχανή.

Απαντήθηκε 04/10/2012 στις 07:04
πηγή χρήστη

ψήφοι
1

Μια λύση είναι να μην χρησιμοποιούν το σύστημα της κατηγορίας γραφομηχανή, αλλά μόνο το Systeme των τύπων και τις διασυνδέσεις, εκτός από τη λέξη «νέο».

    //the function that create class
function Class(construct : Function, proto : Object, ...mixins : Function[]) : Function {
        //...
        return function(){};
}

module Test { 

     //the type of A
    export interface IA {
        a(str1 : string) : void;
    }

    //the class A 
    //<new () => IA>  === cast to an anonyme function constructor that create an object of type IA, 
    // the signature of the constructor is placed here, but refactoring should not work
    //Class(<IA> { === cast an anonyme object with the signature of IA (for refactoring, but the rename IDE method not work )
    export var A = <new () => IA> Class(

        //the constructor with the same signature that the cast just above
        function() { } ,

        <IA> {
            //!! the IDE does not check that the object implement all members of the interface, but create an error if an membre is not in the interface
            a : function(str : string){}
        }
    );


    //the type of B
    export interface IB {
        b() : void;
    }
    //the implementation of IB
    export class B implements IB { 
        b() { }
    }

    //the type of C
    export interface IC extends IA, IB{
        c() : void;
        mystring: string;
    }

     //the implementation of IC
    export var C = <new (mystring : string) => IC> Class(

        //public key word not work
        function(mystring : string) { 

            //problem with 'this', doesn't reference an object of type IC, why??
            //but google compiler replace self by this !!
            var self = (<IC> this);
            self.mystring = mystring;
        } ,

        <IC> {

            c : function (){},

            //override a , and call the inherited method
            a: function (str: string) {

                (<IA> A.prototype).a.call(null, 5);//problem with call and apply, signature of call and apply are static, but should be dynamic

                //so, the 'Class' function must create an method for that
                (<IA> this.$super(A)).a('');
            }

        },
        //mixins
        A, B
    );

}

var c = new Test.C('');
c.a('');
c.b();
c.c();
c.d();//ok error !
Απαντήθηκε 23/01/2013 στις 00:34
πηγή χρήστη

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more