Skip to content

$.fixture templated values not being included in callback data #136

@webbower

Description

@webbower

If you set up a dynamic fixture with a templated URL, the originalOptions param of the callback will not be updated with the templated URL values if the data prop was not set in options of the AJAX call.

$.fixture('GET /todo/{id}', function(orig) {
    console.log(orig.data.id); // undefined
    console.log(orig.data); // undefined
});

$.ajax({
    url: '/todo/10',
    dataType: 'json',
});

I suspect the problem is that here https://github.com/jupiterjs/jquerymx/blob/master/dom/fixture/fixture.js#L62, if you try to merge an object into undefined with jQuery.extend, it fails. So if $.extend(originalOptions.data, data) equates to $.extend(undefined, { id: '10' }), then data isn't created.

The solution I devised involves checking to make sure the data property of originalOptions exists and if it doesn't, setting it to an empty object before calling jQuery.extend.

@@ -53,6 +53,10 @@
            settings.dataTypes.splice(0,0,"fixture");

            if(data){
+               if(! ('data' in originalOptions)) {
+                   originalOptions.data = {};
+               }
+               
                $.extend(originalOptions.data, data)
            }
            // add to settings data from fixture ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions