Pregunta

HTML snippet

<a class="pure-button loveit data-type="image" href="/love/${image?.id}/${session.userId}">..</a>

Canjs which I am new at.

LoveIt = can.Control({


init: function(element, options) {
    var self = this;
    console.log(options);
},

'a.loveit click': function(el, ev) {
    ev.preventDefault();


        var self = this,
            pathname = el.context.pathname,
            getType = $('a.loveit').data('type');

        can.ajax({
            url: pathname + '/type' + '=' + getType,
            success: function(resp) {
                $(self.options.loveit).text(resp.count);
                console.log('success');
            },
            failure: failLove
        });

    console.log(el);
}

});

Try to grab data-type so it could pass to ajax (/love/34?type=image for example, but it keeps giving me /love/34?undefined. not sure where I overlooked.

Appreciate help. thanks in advance.

¿Fue útil?

Solución

For getType you can do el.data('type')

For sending the query string it will be better if you use data:

can.ajax({
        url: pathname,
        data: {
            type: getType
        },
      ...
    });

This will create a proper query string. Make sure that getType has the right value (by doing console.log(getType)) and this should work.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top