Alvaro Alvaro - 1 year ago 85
Javascript Question

Use a knockout component without the view model .JS file?

I'm trying to create a knockout component and load its template from a file like so:

function ordersViewModel(){

function equipmentViewModel(){

ko.components.register('compact-view', {
viewModel: ordersViewModel,
template: {require: '../views/orders/compactTable.html'},
synchronous: true

var MasterModel = function(){
this.orders = new ordersViewModel(), = new equipmentViewModel();

var mm = new MasterModel();

And I'm getting the following error:

Failed to load resource: the server responded with a status of 404 (Not Found)

It seems it is looking for the .js as detailed in the docs:

For this to work, the files
need to exist.

Isn't there a way to use a component without having to separate the viewmodel in another file?

I'm using a MasterModel to be able to call other viewmodel functions from any other viewmodel, and probably separating the
in another file will make things more difficult.

Answer Source

It's definetely possible to do it, you can separately specify the viewModel and the template for a component.

In your case the problem is that you are missing text! before the template path and so RequireJs tries to load it as a javascript resource.

Also, you need to include the RequireJs text plugin:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download