如何使用curry与js-cookie [英] How to use Curry with js-cookie

查看:59
本文介绍了如何使用curry与js-cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并Curry( http://curry.netyou.co.il/)使用js-cookie API( https://github.com/js-cookie/js-饼干),但我遇到了障碍,到目前为止我的实验都没有。我已经能够将货币名称和费率存储到单独的cookie值中,并将它们读回到页面上,但我无法弄清楚如何重新初始化咖喱功能。

I'm trying to incorporate Curry (http://curry.netyou.co.il/) with the js-cookie API (https://github.com/js-cookie/js-cookie), but I've hit a snag and none of my experimenting has worked so far. I have been able to store both the currency name and the rate into separate cookie values, and read them back onto the page, but I can't figure out how to re-initialize the curry function.

选项base似乎不接受变量,而且我不熟悉js / jQuery足以弄清楚如何告诉curry以不同的方式使用cookie变量。我已经能够使用jQuery从cookie中读取货币名称并将选项更改为已选择,但这本身并不会更新价格,我也无法弄清楚如何链接 - 该功能后咖喱的初始化。我的代码如下:

The option "base" doesn't seem to accept a variable, and I'm not versed in js/jQuery enough to figure out how to tell curry to use the cookie variable a different way. I've been able to use jQuery to read the currency name from the cookie and change the 'option' to 'selected', but that itself doesn't update the prices, and I also can't figure out how to chain a re-initialization of curry after that function. My code is as follows:

var savedRate = Cookies.get('site_rate');
var savedCurrency = Cookies.get('site_currency');

$('.currency-list').curry({
    target: '.ov-property-price',
    base:   'AED',
    customCurrency: {
        'AED': 1,
        'EUR': 0.25,
        'GBP': 0.17,            
        'AUD': 0.36,
        'CHF': 0.26,
        'CAD': 0.34,
        'INR': 17.25,
        'IRR': 8015.36
    },
    symbols: {
        'AED': 'AED ',
        'EUR': '€',
        'GBP': '₤',         
        'AUD': 'AU$ ',
        'CHF': 'CHF ',
        'CAD': 'CA$',
        'INR': '₹',
        'IRR': '﷼'
    }
});

$('.currency-list').change(function(){
    var selected = $(this).find(':selected'), // get selected currency
    rate = selected.data('rate'), // get currency rate
    currency = selected.val(); // get currency name

    Cookies.set('site_currency', currency);
    Cookies.set('site_rate', rate);
});

$('.currency-list option[value="' + savedCurrency + '"]').attr('selected', 'selected');

很抱歉提出这样一个基本问题,但我相信解释会让很多其他人受益使用这个插件。

Sorry to ask such a basic question, but I'm confident that an explanation would benefit many other people using this plugin.

非常感谢。

推荐答案

下面的代码应该允许你用保存的货币或默认的起始货币设置咖喱。

The code below should allow you to setup curry with your saved currency or a default starter currency.

// Setup defaults if cookie returns nothing
var savedRate = Cookies.get('site_rate') || 1;
var savedCurrency = Cookies.get('site_currency') || 'AED';

// Save custom currency list
var customCurrency = {
        'AED': 1,
        'EUR': 0.25,
        'GBP': 0.17,            
        'AUD': 0.36,
        'CHF': 0.26,
        'CAD': 0.34,
        'INR': 17.25,
        'IRR': 8015.36
    };

/* Set rate for saved currency
 * If saved rate is the same as in the
 * customCurrency list there is no need for this stage */
customCurrency[savedCurrency] = savedRate;

// Setup curry with your saved rates
$('.currency-list').curry({
    target: '.ov-property-price',
    base:   savedRate,
    customCurrency: customCurrency,
    symbols: {
        'AED': 'AED ',
        'EUR': '€',
        'GBP': '₤',         
        'AUD': 'AU$ ',
        'CHF': 'CHF ',
        'CAD': 'CA$',
        'INR': '₹',
        'IRR': '﷼'
    }
});

这篇关于如何使用curry与js-cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆