2015-02-24 76 views
0

您好林具有與AmountToDisplay()功能我覺得自己像一個模塊的辦法是適當的,但因爲我是新來的OOP我一直有問題的設置,高達有問題從函數返回值

function AmountToDisplay(){ 
    $emitter = $({}); 
    $('#amountItems').change(function(){ 
     var amountchosen = $("#amountItems option:selected").val(); 
     $emitter.trigger('amountUpdate', amountchosen); 
    }); 

    $emitter.on('amountUpdate', function(e, val){ 
     if(val == 2){ 
      return 2; 
     }else if(val == 3){ 
      return 3; 
     }else if(val == 4){ 
      return 4; 
     } 
    }) 
} 
返回值問題

我希望能夠像做

if(AmountToDisplay() ==2){ 
    ..append 2 items to body 
}else if(AmountToDisplay() ==3){ 
    ..append 3 items to body 
} 

console.log(AmountToDisplay())給出不確定

代替退役的urning和我使用警報方法alert(2),它的工作原理。我試圖從選擇框中取出數值,所以我可以從事件中分離出單獨的值,以便我可以在我的代碼other parts in my code in this jsfiddle的其他部分使用該值。在此先感謝您的幫助。

編輯事情是我想能夠使用變化事件的某個地方的金額。所以如果用戶點擊3,我希望能夠獲得該值,這樣即使在用戶輸入時值發生了變化,我也可以執行與更改事件無關的其他功能。發佈/訂閱?

+0

您不能對返回語句使用異步結果。閱讀關於javascript異步執行的更多內容。 (您可能感興趣的一種工具稱爲承諾) – doldt 2015-02-24 07:40:57

+0

(同時考慮您當前的return語句的作用:它不是返回的amountToDisplay函數,而是'amountUpdate'事件的匿名事件處理程序) – doldt 2015-02-24 07:42:45

+0

感謝信息傢伙。我想知道這是否可以以模塊模式的方式完成。例如爲了創建一個私有變量'amountreturned'並且創建一個方法來改變變量並且該變量被返回(public),所以我可以使用'if(amountToDisplay.amountreturned == 2){show 2}'因爲我沒有認爲這是可能的。我認爲所有的答案都將連接變化事件,並將緊密結合。我想要更好的OOP。這是有幫助的。 – 2015-02-24 08:02:51

回答

0

嘗試通過後門。使用參數來欺騙你想要的變量。

$emitter.on('amountUpdate', function(e, val){ 
      console.log(arguments); 
      if(val == 2){ 
       return 2; 
      }else if(val == 3){ 
       return 3; 
      }else if(val == 4){ 
       return 4; 
      } 
     }) 

你可以看到你想要的變量是在參數[1] 另外這裏返回任何東西不服務功能。它確實沒有什麼。

您需要將它傳遞給事件處理函數。

所以通過修改你這樣的代碼:

$emitter.on('amountUpdate', function(e, val){ 
     val = arguments[1]; 
     if(val == 2){ 
      myEventHandlerfor2(val,e); 
     }else if(val == 3){ 
      myEventHandlerfor3(val,e); 
     }else if(val == 4){ 
      myEventHandlerfor4(val,e); 
     } 
    }) 

編輯顯示如何把它放在一個對象

function FruitBasket() { 
    this.apples = 0; 
    this.pears = 0; 
    this.bananas = 0; 
    this.iwonteatthat = 0; 
} 
FruitBasket.prototype.addFruit = function(which) { 
    switch(which) { 
     case 0 : this.apples++;break; 
     case 1 : this.pears++;break; 
     case 2 : this.bananas++;break; 
     case 0 : this.iwonteathat++;break; 
    } 
} 
FruitBasket.prototype.registerManipulator = function(manipulator) { 
    if(!(manipulator instanceof jQuery)) { 
     manipulator = $(manipulator); 
    } 
    manipulator.on('amountUpdate', jQuery.proxy(function(e, val){ 
     val = arguments[1]; 
     this.addFruit(val); 
    },this); 

} 

myFruitBasket = new FruitBasket(); 
myFruitBasket.registerManipulator($('#formfieldstuff')); 
myFruitBasket.registerManipulator(document.getElementById('OtherManipulator')); 
+0

我喜歡這個,但是沒有辦法讓這個不那麼耦合。我看到我無法獲得返回的值,我正在爲發佈/子類型的模式做準備。我想知道這是否可以以模塊模式的方式完成。例如使一個私人變量'amountreturned',並作出一個方法,改變變量和該變量返回(公共),所以我可以使用if(amountToDisplay.amountreturned == 2){顯示2},因爲我不認爲這是可能。你的答案是有幫助的,這是迄今爲止最好的一個 – 2015-02-24 08:12:08

+1

我認爲你正在接近這個錯誤的方式。事件是推動的事情。但是如果你想把它放在一個對象中以備將來使用,你可以使用jquery proxy來設置它來處理對象。我會用一個東西來更新這個例子。 – Tschallacka 2015-02-24 08:22:45

0

那你有沒有回報只有退出的功能事件,但不是函數AmountToDisplay。

  1. 的事件必須是功能外
  2. 該功能只需要與amountItems
  3. 的執行console.log或你的邏輯返回選擇的值必須在事件中

http://jsfiddle.net/mdf083Lb/2/

function AmountToDisplay(){ 
    return $("#amountItems option:selected").val(); 
} 
$("select").change(function(){ 
    console.log(AmountToDisplay()); 
}); 
0

你不需要defina事件的方法或別的東西,你可以通過使用jquery選擇器獲得選擇框的選定值:

var amountToDisplay = $("#amountItems option:selected").val(); 
+0

當用戶選擇不同的金額時,amountTodDisplay不會更改。這就是爲什麼我需要改變方法。 – 2015-02-24 08:07:35