更新javascript对象内的值

时间:2016-02-08 15:00:25

标签: javascript javascript-objects

我正在使用Affirm javascript API,我需要能够更新checkout对象中的值,但我很难这样做。我已经尝试过提到的here,但它没有用。

基本上,对象看起来像这样:

affirm.checkout({  
   "merchant":{  
      "user_confirmation_url":"https://example.com/checkout/",
      "user_cancel_url":"https://example.com/exit"
   },
   "config":{  
      "financial_product_key":"XXXXXXXXX"
   },
   "shipping":{  
      "name":{  
         "full":"Blah Person"
      },
      "address":{  
         "line1":"123 example street",
         "city":"Blah",
         "state":"IL",
         "zipcode":"12345",
         "country":"US"
      }
   },
   "billing":{  
      "name":{  
         "full":"Dirty Larry"
      },
      "address":{  
         "line1":"123 blah street",
         "city":"foo",
         "state":"IL",
         "zipcode":"12345",
         "country":"US"
      }
   },
   "items":[  
      {  
         "display_name":"Example Product",
         "sku":"123",
         "unit_price":"1222",
         "qty":"1",
         "item_image_url":"https://example.com/kitty.jpg",
         "item_url":"https://example.com/product/123"
      }
   ],
   "discounts":{  
      "discount_name":{  
         "discount_amount":0
      }
   },
   "metadata":{  
      "shipping_type":"Ground"
   },
   "order_id":"XXXXXXXXXXXXXXXXXX",
   "shipping_amount":0,
   "tax_amount":0,
   "total":67599
});

以上都是在第一页加载时设置的,但客户仍然可以更新购物车中的商品,因此我需要将这些更改添加到上述对象中。

我已尝试affirm_checkout["shipping_amount"] = 123但未更新运费总额。 affirm_checkout.shipping_amount = 123也不能有人告诉我我做错了什么吗?

2 个答案:

答案 0 :(得分:0)

您应该将checkout对象定义为affirm.checkout函数上下文之外的变量。这样,您可以直接访问对象的内容并将其传递给affirm.checkout(yourCheckoutObject);

from contextlib import contextmanager

@contextmanager
def add_to_path(p):
    import sys
    old_path = sys.path
    old_modules = sys.modules
    sys.modules = old_modules.copy()
    sys.path = sys.path[:]
    sys.path.insert(0, p)
    try:
        yield
    finally:
        sys.path = old_path
        sys.modules = old_modules

答案 1 :(得分:-2)

你可以试试这个:

{
merchant: "foo bar",
config: "baz"
}

然后你可以通过

访问它
checkout.merchant = 123