background preloader

Require

Facebook Twitter

Using jQuery UI with AMD | jQuery Learning Center. Note: This documentation refers to functionality made available in jQuery UI 1.11. As of jQuery UI 1.11, all of the library's source files support using AMD. This means that you can manage your jQuery UI dependencies without using Download Builder, and load jQuery UI's source files asynchronously using an AMD loader such as RequireJS. In this article we'll walk through the process of using AMD with jQuery UI. Let's start by discussing the files we'll need. We'll need to download three things to get up and running: jQuery core, jQuery UI, and an AMD loader. While any AMD loader will work, we'll use RequireJS in this article, which you can download from Now that we have the files we need, we have to discuss where to place them. ├── index.html ├── js │ ├── app.js │ ├── jquery-ui │ │ ├── accordion.js │ │ ├── autocomplete.js │ │ ├── button.js │ │ ├── core.js │ │ ├── datepicker.js │ │ ├── dialog.js │ │ └── ... │ ├── jquery.js │ └── require.js.

Plugins · jrburke/requirejs Wiki. RequireJS allows you to write loader plugins that can load different types of resources as dependencies, and even include the dependencies in optimized builds. Some plugins may also work with other AMD loaders like curl and backdraft, compatibility isn't ensured though, some plugins may require access to RequireJS internal methods to work. For info about how to use and/or write plugins, check the documentation. List of plugins curated by the community Official Plugins domReady by James Burke Waits for the DOM to be ready for modifications. File Type Templating jade by Dan Williams For jade templates.handlebars and sc-handlebars by Alex Sexton For (Sproutcore) Handlebars Templatesmicro-templates by Julien Cabanès Uses underscorejs micro-templates implementation.micro-templates Updated fork by Eero Anttila.

Localization/Internationalization l20n by Fernando Gómez Localization with l20n. Unit Testing Script Helpers async by Miller Medeiros Load asynchronous scripts (e.g. Miscellaneous. Understanding AMD & RequireJS. You have probably already noticed that JavaScript is becoming the virtual machine of the web. And it seems like Jeff Atwood’s law, “any application that can be written in JavaScript, will eventually be written in JavaScript” is working.

If you are a developer, I’m sure you are familiar with, as I like to call it, "script hell. " What do I mean by that? Here is what I mean. Common, Apple! Let’s talk about AMD and it’s usage in the wild. Why should we care about modules? Whole API consists of a single function. Define(id? First, two arguments id and dependencies are optional. Here is a sample module that has dependencies on jQuery and jQuery cookie plugin: Loaded dependencies are referenced as arguments to your factory function. The order of loaded modules (if they are properly defined) does not matter. All modules are asynchronous by nature and are loaded into the browser in a non blocking fashion. Not convinced yet? Advantages Let’s summarize what advantages over “script hell” it provides:

RequireJS shim configuration, jQuery plugins and enforceDefine » unitstep.net. If you’re using RequireJS’s shim configuration to load jQuery plugins while setting enforceDefine: true in your configuration, you probably noticed the following uncaught error in your JavaScript console: (The following example is for the iCheck plugin) Uncaught Error: No define call for iCheck Reading the documentation at that URL, yields the following important tidbit. (Emphasis mine) This occurs when enforceDefine is set to true, and a script that is loaded either:Did not call define() to declare a module. Or was part of a shim config that specified a string exports property that can be checked to verify loading, and that check failed.Or was part of a shim config that did not set a string value for the exports config option.

This means if you have probably defined your shim config like so, because you technically don’t need any exports from a jQuery plugin: (Since they just extend the jQuery object) Using jQuery and jQueryUI Widget Factory plugins. (For more resources related to this topic, see here.) We must declare the jquery alias name within our Require.js configuration file. If a jQuery plugin does not register itself as AMD compatible, we must also create a Require.js shim configuration to make sure Require.js loads jQuery before the jQuery plugin. We will now be able to dynamically load a jQuery plugin with the require() method. // Dynamically loads a jQuery plugin using the require() methodrequire(["somePlugin"], function() {// The callback function is executed after the pluginis loaded}); We will also be able to list a jQuery plugin as a dependency to another module. // Sample file// -----------// The define method is passed a dependency array and a callbackfunctiondefine(["jquery", "somePlugin"], function ($) {// Wraps all logic inside of a jQuery.ready event$(function() {});}); "jqueryui": "libs/jqueryui","selectBoxIt": "libs/plugins/selectBoxIt" Next, create a shim configuration property: How it works...

There's more... Jquery. How to Load jQuery Plugins With RequireJS - Simone Web Design. RequireJS is a useful script loader library. Even if your site/app isn’t super big, it’s usually a good idea to use RequireJS: it makes your code maintainable. The problem Often we need to include external plugins that depend on jQuery. Those plugins usually do expect to find a global jQuery ($) object, and then they just extend it. Of course not all plugins behave like that, but most of them do, and we must deal with it. By the way, loading jQuery as a global variable is not really a good idea. The solution Use RequireJS with jQuery to manage dependencies. Let’s suppose you need to include 2 jQuery plugins, foo.js and bar.js. Then, later, when you need to use that Foo plugin, you can just define a module by doing: And require it when you actually want to execute it: Yeah, it’s that simple.

Want to see a working example? Example-jquery-shim/www/js/lib at master · requirejs/example-jquery-shim. How to make a jQuery plugin loadable with requirejs. Full Stack JavaScript: Backbone, Node, Express & More. The Story So, you and your co-founder have this great idea for a business, right? You’ve been adding features in your mind. Frequently, you ask potential customers for their opinions, and they all love it. Ok, so people want it. There’s even some money to be made. So finally, you sit down one day and say, “Let’s do it!” “Done! “Ok, let’s create the site”, you say. And then, you realize the truth: you need to choose a programing language; you need to choose a (modern) platform; you need to choose some (modern) frameworks; you need to configure (and purchase) storage, databases, and hosting providers; you need an admin interface; you need a permissions system; you need a content manager.

You want to be lean, you want to be agile. You have tens upon tens of architectural decisions to make. “I’m overwhelmed,” you say, as you feel overwhelmed. Your proof of concept slowly withers and dies. The Proposal After abandoning tons of ideas myself in this way, I decided to engineer a solution. Mike Dellanoce's Blog: Zero-friction Javascript Minification with RequireJS and Express.

Roll Your Own Controllers with Express and Require.JS - The UrBlog. I’m a Grails developer at work, so I’ve gotten used to convention over configuration for controllers. I just create a class with a name that ends in Controller and place it the controller folder. Each method in the controller is an action and it allows me to access that method via /controller/action/id. While Node and Express have full blown Rails copies (such as Geddy), I want something simpler.

I prefer to use Require.JS to help me manage my dependencies. This gave me an idea. Now any module in app/controller that has a method that takes a request and response can act as a controller with no configuration. There are cases where your URL does not match /controller/action. We can then loop thru the mappings and register them with Express: Requirejs-middleware. Connect middleware for RequireJS compiling Middleware for Connect & Express that runs the RequireJS optimizer on demand. Production-ready and used by Mozilla Popcorn Maker. npm install requirejs-middleware var express = require"express" path = require"path" requirejsMiddleware = require"requirejs-middleware"; var app = express; appuseexpresslogger"dev"; appuserequirejsMiddleware src: pathjoin__dirname + "/public" dest: pathjoin__dirname + "/build" build: true debug: true modules: "/main.js": baseUrl: pathjoin__dirname + "/public" include: "main" appuseexpressstaticpathjoin__dirname + "/build"; appuseexpressstaticpathjoin__dirname + "/public"; applisten3000; See the full example in examples/almond/server.js. src - Source directory containing the RequireJS modules.

Auto-optimization with Express and RequireJS. Requirejs-middleware/server.js at master · jbuck/requirejs-middleware. Jbuck/requirejs-middleware. Writing Modular JavaScript With AMD, CommonJS & ES Harmony. When we say an application is modular, we generally mean it's composed of a set of highly decoupled, distinct pieces of functionality stored in modules. As you probably know, loose coupling facilitates easier maintainability of apps by removing dependencies where possible. When this is implemented efficiently, its quite easy to see how changes to one part of a system may affect another. Unlike some more traditional programming languages however, the current iteration of JavaScript (ECMA-262) doesn't provide developers with the means to import such modules of code in a clean, organized manner.

It's one of the concerns with specifications that haven't required great thought until more recent years where the need for more organized JavaScript applications became apparent. Instead, developers at present are left to fall back on variations of the module or object literal patterns. Today it's embraced by projects including Dojo (1.7), MooTools (2.0), Firebug (1.8) and even jQuery (1.7). App.js. Javascript-playground/backbone-require-example. Using Backbone.js with Require.js - Greg Franko. Backbone.js is a great client-side MV* (not MVC) JavaScript framework that provides structure to JavaScript applications by providing View, Model, Collection, and Router classes. Backbone also provides a pub sub (publish subscribe) mechanism, by allowing each of it’s objects to trigger and bind to events. Backbone Views act as a combination of traditional MVC Views and Controllers, since they allow you to organize all of your JavaScript event handlers while also providing a mechanism for adding dynamic HTML to your page through the optional use of JavaScript templates.

Views will also often be where you set data on your Models. Here is an example Backbone View class: Backbone Models store all of your applications business logic and data. Here is an example Backbone Model class: If your application is complex, it is also helpful to use Backbone Collections, which provides a mechanism to interact with many different Model instances. Here is an example Backbone Collection class: SlexAxton/require-handlebars-plugin. Using Handlebars with Backbone and RequireJS and Precompiling templates. Note: I did discover something a little strange...that this process doesn't work if there is only one template file that you are compiling. When my 'templates' dir had more than one .hbs file in it, then everything worked just fine. Not sure why that is. Even more strange: I later found that even if you only have one template to compile, you can still get things to work by changing your syntax in your view.

In my example below (the backbone view), I set the 'template' property to Templates['my-view.hbs']. I found that you can also set the value to Handlebars.templates['my-view.hbs']. In this case, it will work even if you have only one template file that gets compiled. 1. 2. Npm install -g handlebars 3. Handlebars --amd *.hbs -f compiled-templates.js 4. Define(['backbone','underscore','jquery', "../.. function ( Backbone, _, $, Templates) { var MyView = Backbone.View.extend({ template: Templates['my-view.hbs'], render: function(){ return this; return MyView; 1. 2. 3. 4. 5.

<h3>{{someVar}}</h3> 6. Guybedford/require-less. Guybedford/require-less. Using The RequireJS Build / Optimizer To Concatenate Modularized CSS Files. A little while back, I started looking at RequireJS as away to organize and modularize my JavaScript code. And, while I am still getting my feet wet in modular JavaScript web application development, I can tell you that I have really enjoyed using RequireJS - and, that I plan to continue to integrate it into my development process. The asynchronous loading and the on-demand loading of JavaScript modules are nice feature of RequireJS; but the real "ah ha! " moment for me came with the ability to organize my JavaScript code into small(er) cohesive files. In my reading, I noticed that the RequireJS optimizer - r.js - also works with CSS (Cascading Style Sheets) files. As such, I wanted to see if RequireJS could afford the same kind of benefits in the world of CSS that it provided in the world of JavaScript.

The functionality of the RequireJS optimizer as it applies to CSS is rather simple. Optimizes CSS by inlining CSS files referenced by @import and removing comments. API. Usage§ 1 Load JavaScript Files§ 1.1 RequireJS takes a different approach to script loading than traditional <script> tags. While it can also run fast and optimize well, the primary goal is to encourage modular code. As part of that, it encourages using module IDs instead of URLs for script tags. RequireJS loads all code relative to a baseUrl. The baseUrl is normally set to the same directory as the script used in a data-main attribute for the top level script to load for a page. Or, baseUrl can be set manually via the RequireJS config. RequireJS also assumes by default that all dependencies are scripts, so it does not expect to see a trailing ".js" suffix on module IDs. There may be times when you do want to reference a script directly and not conform to the "baseUrl + paths" rules for finding it.

Ends in ".js".Starts with a "/".Contains an URL protocol, like "http:" or "https:". In general though, it is best to use the baseUrl and "paths" config to set paths for module IDs. In index.html: Improving frontend web performance using RequireJS optimiser.