2016-04-06 11 views
0

Gibt es eine Möglichkeit, dies in Coffeescript umzuformen?Refactoring in verschiedene Arrays pushen

class Article 
    constructor:() -> 
     @rims = [] 
     @tyres = [] 
     @others = [] 
     @wheels = [] 

    addRim: (id) -> 
     product = new SimpleProduct(id) 
     @rims.push(product) 
     product 

    addTyre: (id) -> 
     product = new SimpleProduct(id) 
     @tyres.push(product) 
     product 

    addOther: (id) -> 
     product = new SimpleProduct(id) 
     @others.push(product) 
     product 

    addWheel: (rimId, tyreId) -> 
     wheel = new Wheel(rimId, tyreId) 
     @wheels.push(wheel) 
     wheel 

Antwort

1

hinzufügen, diese Funktionen Satz integriert werden kann eine

class Article 
    constructor:() -> 
     @rims = [] 
     @tyres = [] 
     @others = [] 
     @wheels = [] 

    add: (aryName, model, args...) => 
     m = new model(args...) 
     @[aryName].push m 

class Rim 
    constructor: (args...) -> 
    console.log args 

class Tyre 
    constructor: (args...) -> 

a = new Article() 

a.add('rims', Rim, 'a','b','c') 
0

Die drei identische Funktionen ihrer Syntax, um ein Bit könnte unter Verwendung eines anderen generic addProduct Methode, die eine variable vertrocknet (ein Array) führt und schiebt ein neues einfaches Produkt dazu. Ihr addWheel sieht anders aus, um eine separate Methode zu gewährleisten. Ich denke, die beste Lösung ist wahrscheinlich, etwas zu tun, wo addWheel die Felgen erstellt, aber ich bin mir nicht sicher, wie Sie dieses Modell verwenden.

class Article 
    constructor:() -> 
     @rims = [] 
     @tyres = [] 
     @others = [] 
     @wheels = [] 

    addProduct: (variable, id) -> 
     variable.push(product = new SimpleProduct(id)) 
     product 

    addRim: (id) -> 
     this.addProduct(@rims, id) 

    addTyre: (id) -> 
     this.addProduct(@tyres, id) 

    addOther: (id) -> 
     this.addProduct(@others, id) 

    addWheel: (rimId, tyreId) -> 
     wheel = new Wheel(rimId, tyreId) 
     @wheels.push(wheel) 
     wheel 

Die Tatsache, dass Räder Felgen und Reifen, können Sie nicht separate Instanz-Variablen auf Ihrem Modell benötigen für Felgen und Reifen, ist es besser sein kann, sie so zu verschachteln:

class Article 
    constructor:() -> 
     @others = [] 
     @wheels = [] 

    addOther: (id) -> 
     @others.push(product = new SimpleProduct(id)) 
     product 

    addWheel: (rimId, tyreId) -> 
     wheel = new Wheel(rimId, tyreId) 
     @wheels.push(wheel) 
     wheel 

    rims: -> 
     @wheels.map((wheel) -> wheel.rim) 

    tyres: -> 
     @wheels.map((wheel) -> wheel.tyre) 

class Wheel 
    constructor: (rim, tyre) -> 
     @rim = new SimpleProduct(rim) 
     @tyre = new SimpleProduct(tyre) 

Hier Sie können folgendes tun:

a = new Article() 
a.addWheel(2,3) 
a.tyres() // returns [SimpleProduct(2)] 

Dieses zweite Beispiel ist nur, wenn Sie this.tyres für den Zugriff auf die Reifen zu verwenden versuchen, die die addWheel() metho verwenden hinzugefügt d. Soweit ich weiß, verwenden Sie unterschiedliche Reifen, Felgen und Räder (das sind Reifen und Felgen).