2016-09-01 14 views
0

私はノックアウト観測可能性があります:self.productBarcode = ko.observable()、製品が見つかった場合はjqueryオートコンプリートを使用してjqueryオートコンプリートを使用して選択イベントがあります観測可能に選択したオブジェクトを追加するには、オートコンプリート機能:ノックアウト観測のアクセスプロパティ

select: function (event, ui) { 
       updateElementValueWithLabel(event, ui); 
       self.productBarcode(ui); 

UIオブジェクトの形式は次のとおりです。

ui 
{ 
    item 
    { 
     barcode: "2" 
     label: "p1" 
     value: "p1" 
    } 
} 

その後、私は必要なものSELにあります製品番号バーコードproductBarcodeは、uiと同じ形式です。

問題:私が観察productBarcodeからバーコードプロパティにアクセスするにはどうすればよいですか? 私はfolowing試してみた:

self.addNewSale = function() { 
    var placeNewSale = { 
     StoreId: self.SaleStoreObject().Id, 
     StoreName: self.SaleStoreObject().Name, 
     ProductBarcode: self.productBarcode().barcode, 
     ProductName: self.productBarcode().label, 
     Quantity: self.newSale.Quantity() 
    } 

    self.placeSaleProducts().push(placeNewSale); 
    self.placeSaleProducts(self.placeSaleProducts()); 
} 
+0

あなたが求めていることを完全にはっきりさせることはできません。期待される動作は何ですか?そしてあなたの現在のコードの結果は何ですか? (また、 '.push'の前に'() 'を省略することによって直接' observableArray'にプッシュできることに注意してください) – user3297291

+0

** ui *の形式を持つオブジェクトからbarcodeプロパティにアクセスしようとしています。 * – sixfeet

+1

「obj.item.barcode」と似ていますか? – user3297291

答えて

1

あなたはko.observableそうのように定義する場合:

self.productBarcode = ko.observable(); 

その初期値はundefinedになります。

あなたがチェックすることができ...これはそれができない、undefineditemプロパティにアクセスしようとしたJavaScriptにつながる

var currentBarcode = self.productBarcode().item.barcode; 

:これは、あなたが盲目的ような何かをすることによって、そのプロパティにアクセスすることができないことを意味しますundefined、またはより短いが、あまり "安全" falseyチェックして行く:それは

// Option 1: Explicitly check for undefined: 
var current = self.productBarcode(), 
    currentBarcode = (typeof current !== "undefined") ? current.item.barcode : null; 
// Option 2: Check if there's "something truthy" in the observable 
var current = self.productBarcode(), 
    currentBarcode = current ? current.item.barcode : null; 
関連する問題