Mark
Mark

Reputation: 7818

KnockoutJS newly added element does not have any styling from the stylesheet

I'm trying to use KnockOut, similar to this page: http://knockoutjs.com/examples/cartEditor.html

However, when used with the JQuery Mobile theme - when you select an item from the Category drop down, it then adds a drop down list under the Product heading - BUT the second dropdown just added, doesn't have the mobile styling applied.

I've added a Fiddle here: http://jsfiddle.net/mtait/adNuR/1927/ - to demonstrate. What can I add that will add the styling to the new drop down list?

function formatCurrency(value) {
return "$" + value.toFixed(2);
}

var CartLine = function() {
var self = this;
self.category = ko.observable();
self.product = ko.observable();
self.quantity = ko.observable(1);
self.subtotal = ko.computed(function() {
    return self.product() ? self.product().price * parseInt("0" + self.quantity(), 10) : 0;
});

// Whenever the category changes, reset the product selection
self.category.subscribe(function() {
    self.product(undefined);
});
};

var Cart = function() {
// Stores an array of lines, and from these, can work out the grandTotal
var self = this;
self.lines = ko.observableArray([new CartLine()]); // Put one line in by default
self.grandTotal = ko.computed(function() {
    var total = 0;
    $.each(self.lines(), function() { total += this.subtotal() })
    return total;
});

// Operations
self.addLine = function() { self.lines.push(new CartLine()) };
self.removeLine = function(line) { self.lines.remove(line) };
self.save = function() {
    var dataToSave = $.map(self.lines(), function(line) {
        return line.product() ? {
            productName: line.product().name,
            quantity: line.quantity()
        } : undefined
    });
    alert("Could now send this to server: " + JSON.stringify(dataToSave));
};
};

ko.applyBindings(new Cart());

Thank you,

Mark

Upvotes: 0

Views: 184

Answers (1)

PeaceFrog
PeaceFrog

Reputation: 717

I think you need a customer binding handler.

Handler:

ko.bindingHandlers.jqmOptions = {
    update: function (element, valueAccessor, allBindingsAccessor, context) {
        ko.bindingHandlers.options.update(element, valueAccessor, allBindingsAccessor, context);
        $(element).selectmenu();
        $(element).selectmenu("refresh", true);
    }
};

Binding:

<select data-bind='jqmOptions : sampleProductCategories, optionsText: "name", optionsCaption: "Select...", value: category'> </select>

Fiddle: http://jsfiddle.net/adNuR/1942/

Upvotes: 1

Related Questions