asp.net mvc 3 - Loading "knockout.mapping" plugin using require.js -
i creating mvc3 application, requirejs. in views need convert model object knockout viewmodel object. need use knockout , knockout.mapping libraries.
my application designed in following way,
1). script files categorized folders
- scripts/app/home/ - contains scripts views in home controller.
- scripts/lib/ - contains scripts jquery, knockout,knockout.mapping, requirejs etc
2). in "_layout.cshtml" referencing "require.js" this.
<script src="@url.content("~/scripts/lib/require.js")" type="text/javascript"></script>
3). configure require.js settings using different script file called "common.js" (scripts/lib/common.js)
require.config( { baseurl: "/scripts/", paths:{ jquery: "lib/jquery-2.0.3", ko: "lib/knockout-2.3.0", komapping: "lib/knockout.mapping" } });
4). index.js file in 'scripts/app/home/"
define(['ko', 'komapping'], function (ko, komapping) { var person = function () { var self = this; self.getpersonviewmodel = function (data) { return ko.mapping.fromjs(data); ; }; }; return { person: person };
});
5). "index" action method in "home" controller
public actionresult index() { var person = new person { id = 1, name = "john", addresses = new list<address>(new[]{new address{country = "country 1", city = "city 1"}}) }; return view(person); }
6). "index" view
@model mms.web.models.person <script type="text/javascript"> require(["/scripts/common/common.js"], function () { require(["app/home/index"], function (indexjs) { var person = new indexjs.person(); var vm = person.getpersonviewmodel(@html.raw(json.encode(model))); }); }); </script>
the problem facing when loading index.js file, script error knockout.js cannot loaded.
failed load resource: server responded status of 404 (not found) - http:///scripts/knockout.js
but if remove dependency of "komapping" inside "index.js" file loads correctly, cannot use mapping functionality.
i had inside these links, couldn't find solution, knockout.js mapping plugin require.js , https://github.com/stevesanderson/knockout.mapping/issues/57
help, suggestions appreciated. thanks!
i had same issue. problem knockout.mapping defines knockout dependency, need satisfy 1 when load script.
here how should load mapping stuff
require.config( { baseurl: "/scripts/", paths:{ jquery: "lib/jquery-2.0.3", knockout: "lib/knockout-2.3.0", komapping: "lib/knockout.mapping" }, shim: { komapping: { deps: ['knockout'], exports: 'komapping' } } });
then in case, use index.js file requirejs call following
requirejs(['jquery', 'knockout', 'komapping'], function($, ko, komapping){ ko.mapping = komapping; //do other stuff here });
Comments
Post a Comment