1
I’m trying to load the template and Viewmodel to a knockout component using require.js, but so far unsuccessfully.
index.html (view)
<!doctype html>
<head>
<link href="assets/css/bootstrap.min.css" rel="stylesheet" />
<script data-main="assets/js/app" src="assets/js/vendor/require.js"></script>
</head>
<body>
<table_n></table_n>
</body>
menu.html (Template)
<table class="table table- bordered table-hover">
<thead>
<tr>
<th align=center width=60 style="display: none">Código</th>
<th>Título</th>
<th>Descrição</th>
<th>Ordem</th>
<th>Imagem url</th>
</tr>
</thead>
<tbody data-bind="foreach: {data: ListaUsuarios()}">
<tr>
<td style="display: none" data-bind="text: id"></td>
<td data-bind="text: titulo"></td>
<td data-bind="text: descricao"></td>
<td data-bind="text: ordem"></td>
<td data-bind="text: caminho_imagem"></td>
</tr>
</tbody>
</table>
menu.js (Viewmodel)
define(['knockout', 'knockoutMapping'], function (ko, komap) {
debugger;
var self = this;
debugger;
self.filter = ko.observable('');
self.ListaUsuarios = ko.observableArray();
$.ajax({
type: "GET",
url: "http://192.168.15.3/api/menus",
contentType: "application/javascript",
dataType: "json",
success: function (result) {
var a = JSON.stringify(result);
var observableData = komap.fromJS(result);
var array = observableData();
self.ListaUsuarios(array);
}
});
});
App.js (Initiating parameters in knockout)
(function (undefined) {
'use strict';
requirejs.config({
baseUrl: './', // Raiz
urlArgs: function (id, url) {
return (url.indexOf('?') === -1 ? '?' : '&') + 'v=23';
}, // Debug Cache
deps: ['assets/js/base'],
map: {
'*': {
'jQuery': 'jquery'
}
},
paths: {
// Módulos
'jquery': 'assets/js/vendor/jquery-3.1.1',
// Library jQuery
'knockout': 'assets/js/vendor/knockout-3.4.2',
'knockoutMapping': 'assets/js/vendor/knockout.mapping-latest',
// Config
'start': 'assets/js/start'
},
shim: {
'knockoutMapping': {
deps: ['knockout']
}
},
waitSeconds: 15
});
// Chamando módulo principal para iniciar a aplicação
require(['jquery'], function ($) {
require(['start']);
});
requirejs.onError = function (err) {
console.log(err.requireType);
console.log('modules: ' + err.requireModules);
throw err;
};
}());
base.js (Using jQuery as a module called in Requirejs)
(function () {
define(['jquery'], function () {
(function ($) {
console.info('Verificando Global jQuery...');
if (typeof window === 'object' && typeof window.document === 'object') {
if (!!window && !(!!window.$)) {
window.jQuery = window.$ = jQuery;
}
console.log([$, jQuery]);
}
var version = $().jquery;
if (typeof define === "function" && define.amd && define.amd.jQuery) {
console.info('jQuery: ' + version + ' $.fn.jquery: ' + $.fn.jquery);
return window.jQuery;
}
}(jQuery));
});
}());
start.js (and finally initiating the component)
define(['knockout', 'knockoutMapping'], function (ko, komap) {
debugger;
ko.components.register('table_n', {
viewModel: { require: 'assets/js/component/viewmodel/menu' },
template: { require: 'text!assets/js/component/templates/menu.html' }
});
ko.applyBindings();
});
And down with the sad mistakes!