Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AMD/RequireJS support #57

Closed
sgentile opened this issue Feb 16, 2012 · 13 comments
Closed

AMD/RequireJS support #57

sgentile opened this issue Feb 16, 2012 · 13 comments

Comments

@sgentile
Copy link

The lastest branch of Knockout supports AMD/RequireJs. While converting your code to also use AMD/RequireJS ran into some issues. The knockout.mapping has the following function in it:

ko.exportSymbol = function (publicPath, object) {
var tokens = publicPath.split(".");
var target = window;
for (var i = 0; i < tokens.length - 1; i++)
target = target[tokens[i]];
target[tokens[tokens.length - 1]] = object;
};

and it uses the var target = window as the start. knockout.js has a
similar function, but it uses target = ko as the start.

I'd like to ask for that change, and
that way the ko.exportSymbol() usage at the bottom of the file can be
reduced. It is unclear though that any of that export symbol stuff is
needed at all in the mapping file since it starts at the top of the
file and does a ko.mapping = {} anyway.

@sagacity
Copy link
Collaborator

Hi Steve, Domenic Denicola just provided us with a module wrapper. It's now part of the master branch, maybe you can take a look and see if it already meets your needs?

@sgentile
Copy link
Author

Great - Thank you !

@younes200
Copy link

Hi, Is there a example to how to use knockout and knockout.mapping with requiresjs ? Here the code I'm using but with no luck :

define([ "order!jQuery"
       , "order!libs/knockout/knockout"         
       , "order!libs/knockout/knockout.mapping"
       ]
       , function() {
           return ko; 
         }
      );

I'm getting : Load timeout for modules: knockout http://requirejs.org/docs/errors.html#timeout

Thank you

@sgentile
Copy link
Author

sgentile commented Mar 6, 2012

the latest branch has native support for RequireJS - as well as the latest mapping - so you don't need the 'order' plugin

@sgentile
Copy link
Author

sgentile commented Mar 6, 2012

require({
paths: {
jqueryvalidate: 'libs/jquery/jquery.validate',
jqueryui: 'libs/jquery/jquery-ui-1.8.11.min',
bootstrap: 'libs/jquery/bootstrap',
knockout: 'libs/knockout/knockout-latest.debug',
komapping: 'libs/knockout/knockout.mapping-latest.debug',
}
}, ['csmain'], function ($) {

});

in 'main' then something like this:
define(['jquery', 'knockout', 'komapping'], function($, ko, komapping) {
...
}

@younes200
Copy link

Thank you, works fine !

@Gooseus
Copy link

Gooseus commented Mar 22, 2012

You guys don't seem to be using the order plugin to load knockout before knockout.mapping, how are you handling the dependency? I'm trying to get them working together and am getting "ko is null" when knockout.mapping is loaded

require(['jquery', 'jquery.form', 'jquery.colorbox.min', 'jquery.tmpl.min', 'jquery.easing.1.3', 'jquery.bxSlider.min', 'knockout', 'knockout.mapping.debug', 'q.min', 'js/humane.min.js'],
function($,ko,komap,q) {
        // Never get's here - 'ko is null on line 16'
    console.log([$,ko,komap,q,window.humane]);

});

@domenic
Copy link
Contributor

domenic commented Mar 22, 2012

Your module ID <-> parameter map seems incorrect. You'd have to fix it like so:

require(['jquery',
        'jquery.form',
        'jquery.colorbox.min',
        'jquery.tmpl.min',
        'jquery.easing.1.3',
        'jquery.bxSlider.min',
        'knockout',
        'knockout.mapping.debug',
        'q.min',
        'js/humane.min.js'],
        function($, jQueryForm, jQueryColorBox, jQueryTmpl, jQueryEasing, jQueryBxSlider, ko, komap, q) {

    console.log([$,ko,komap,q,window.humane]);

});

or like so (my preferred approach, if you can switch to define instead of require):

define(['jquery',
        'jquery.form',
         'jquery.colorbox.min',
         'jquery.tmpl.min',
         'jquery.easing.1.3',
         'jquery.bxSlider.min',
         'knockout',
         'knockout.mapping.debug',
         'q.min',
         'js/humane.min.js'],
        function(require) {

    var $ = require("jquery");
    var ko = require("knockout");
    var komap = require("knockout.mapping.debug");
    var q = require("q.min");

    console.log([$,ko,komap,q,window.humane]);
});

@Gooseus
Copy link

Gooseus commented Mar 22, 2012

I forgot to mention that I'm using the require-jquery.js from here:

http://requirejs.org/docs/jquery.html

So I shouldn't need to specify the jquery plugins separately in the arguments, other than that I don't see a difference between your first example and mine? I'll give your second approach a shot, though I'm getting the error in the ko.mapping so my callback doesn't even run.

@domenic
Copy link
Contributor

domenic commented Mar 22, 2012

There is a one-to-one mapping between elements of the array and arguments to the callback. So in your example, ko (the second argument) is getting the exports from jquery.form (the second element of the array), which are null.

@Gooseus
Copy link

Gooseus commented Mar 22, 2012

I simplified it to remove all the other modules and made sure they're ordered correctly:

require(['jquery', 'knockout', 'knockout.mapping.debug'],
function($,ko,komap) {
    console.log([$,ko,komap]);

});

I'm still getting the same 'ko is null' error in knockout.mapping.debug

@Gooseus
Copy link

Gooseus commented Mar 22, 2012

Ah, looks like not... I had been using version 2.0.0 for knockout and saw a define function the source and a pull request for RequireJS support in 1.3.1 so I thought I was good.

Thank you sir for prompt assistance, I seem to be in business!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants