how to use requirejs for non single page web apps? - codeigniter

Backbone is a must to organize our javascript code, I've been using it in non-singlepage applications but until now I've been structuring my codeigniter application as follows:
/
/profile
/activity
/....
Where some of the pages share javascript functions, backbone views,backbone models, etc
what i'm doing when I've javascript functionality that is shared across multiple pages is include it on a main.js
/profile
...jquery.js
...backbone.js
...other stuff...
...main.js <--- shared scripts
...page.js <--- specific page script (e.g profile.js)
The problem is when I've already a model that i would like to use in another javascript specific page (but not in all other pages) I've to replicate that piece of code into other-page.js.
That's annoying and a more scalable and modular solution would be great.
But i'm not used with require.js so I'm here asking for patterns that might help organizing my code.
Thank you very much.

You can use requirejs modules and create multiple modules for multiple pages and a common module for your common stuff:
Each page uses a mix of common and page-specific modules.
All pages share the same requirejs config.
After an optimization build, the common items should be in a shared common layer, and the page-specific modules should be in a page-specific layer.
The configuration for this could be (build.js):
{
// ther 'regular' configuration,
modules: [{
name: '../common',
include: ['jquery', 'app/lib', 'app/controller/Base','app/model/Base']
},{
name: '../page1',
include: ['app/main1'],
exclude: ['../common']
},{
name: '../page2',
include: ['app/main2'],
exclude: ['../common']
}]
}
check out https://github.com/requirejs/example-multipage for more details

Related

webpack2 i18n-plugin How to decide the bundle to load

I have a working webpack config which can generate i18n bundles. I am using i18n-webpack-plugin. My website is a static website. I can see the bundles generated as [language].[name].bundle.js. eg: "de.login.bundle.js", "en.login.bundle.js".
Now in my login html page, how do I decide which of these bundles to load ? I will provide my users an option for language selection, say in top nav bar. Once user has selected it, how do I load the appropriate language bundle?
I found a solution to achieve this using HtmlWebpackPlugin :
new HtmlWebpackPlugin({
filename: language + '/login.html',
template: 'html/login.html',
chunks: ['login']
})
This generates separate Html files under each language directory eg: en/login.html, de/login.html. Now , based on the user's language selection, he can be redirected to appropriate URL.
I would still be interested to know if there's any alternate way to achieve this.

Using Individual pieces of Backbone Marionette with RequireJS

I am looking to use Backbone Marionette in my application with RequireJS. It looks very promising,especially the Views and Regions. I would like to use them in my application but use some of the existing features already present in my application.
For example,I would like to use my existing Event Aggregator instead of Backbone.Wreqr. Can I do that? Is Backbone.Wreqr hard dependency for Marionette or can Marionette work without it?
Also I would not like to include pieces of Marionette that I do not plan to use in my application, like modules, templateCache, etc.
Is it possible to have a trimmed version of Marionette?
Thanks
Chintan
Currently, Marionette has hard dependencies on multiple libraries. From it's documentation:
Prerequisites
Marionette relies on Underscore, Backbone, jQuery, and various other libraries as its foundation.
JSON2.js
jQuery (v1.7, v1.8, v1.9)
Underscore.js (v1.4.4)
Backbone.js (v1.0.0)
backbone.wreqr.js
backbone.babysitter.js
As far as picking and choosing features, Marionette's code is split up nicely with each main feature in it's own file. You might be able to copy the repo and do one of the following:
AMDify each feature to use with requirejs
Piece together the files/features that you want and amdify the resulting combined file
Rip out the features that you don't want from the generated amdified file.
With any of the routes, be sure to get the dependencies between the features lined up correctly (i.e. Layout depends on ItemView, which depends on View, so you'll need to keep View and ItemView if you intend to use Layout).

Selectively disabling plugins from loading in Wordpress admin area

Does anybody know how to disable certain plugins from loading in the back-end/admin area of Wordpress?
Currently I'm running a site that has many plugins mainly geared towards front-end manipulation yet when I access pages in the backend (ie. /wp-admin/edit.php) all CSS, JS and plugin files are being loaded for plugins that are not required there, thus increasing the load-time and responsiveness of the admin area.
I'm looking for a solution, either plugin based for code that can selectively load admin only plugins, ideally without having to hack the core files.
I'm using wordpress 3.5.1.
Checkout Plugin Organizer. I haven't used it but according to it's description, you can "Selectively disable plugins by any post type or wordpress managed URL". I would guess that you could disable certain plugins from running on urls that contain /wp-admin/.
You can also modify the plugins themselves. It depends how they are written, but you can find where they are enqueuing the css and js files and wrap those in an is_admin() statement like this:
// Make sure we aren't in the admin area
if ( !is_admin() ) {
wp_enqueue_script('plugin-script');
wp_enqueue_style('plugin-style');
}
That will ensure that the scripts/styles are only loaded on the front end.
Another possibility is to find all of the css and script files that are being loaded via plugins and deregister them in your functions.php file. This will require you to poke around the plugins a bit to find the handles of all the files, but it should work well. This would deregister some of the default stuff enqueued in the admin area, so you can see what I mean.
add_action( 'admin_init', 'remove_admin_styles' );
function remove_admin_styles() {
wp_deregister_style(
'wp-admin',
'ie',
'colors',
'colors-fresh',
'colors-classic',
'media',
'install',
'thickbox'
);
}
As you said, you don't want to mess with core files, but if you absolutely need to you can implement the solution described in this article. As I'm sure you know, it's not a good idea to alter WordPress core files unless you absolutely have to. Just keep in mind that the changes will be wiped out if you upgrade WordPress in the future.

Web Application Architecture Approach CodeIgniter

I've been searching for a good architecture models for some time. I think that in a good application should have controllers and modules which should be repeatable parts on different pages. For example a shopping cart - if you are working on an online store you would need it on almost every page.
Also another requirement for me is that it should support changing styles (themes|skins) of the website easily - which can be achieved by deciding the style in two parts - views and assets (css, images, javascript). Which means that all the views should be located on one place, not like in Modular Extension.
And finally it's directory structure should look like this:
application/
├────controllers/
| ├────home.php
| └────products.php
├────modules/
| └────shopping-cart.php
└────views/
└────style_blue/
├────home.php
├────products.php
└────modules/
└────shopping-cart.php
assets/
└────style_blue/
├────css/
| └────style.css
├────js/
| └────jquery.js
└────images/
└────header.png
This is the perfect architecture isn't it?
It can be extended with new modules and controllers.
It can have different styles (skins|themes)
It is pretty simple and in the same time functional
I am a big fan of CodeIgniter and I want to achieve something like that with it. Is there an add-on which could work for me?
You could check http://www.getsparks.org for some add-ons.
I believe this is what you need for your custom styles:
http://getsparks.org/packages/template/versions/HEAD/show
It can handle multiple themes according to the description. Furthermore this library is very handy. (I am using it, just didn't use the theme part yet).
Furthermore, for modules maybe this is what you need?
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home

How to manage files for a module in ASP.net MVC 3

I built a small module in ASP.net MVC which consists of
a javascript file
a css file
a razor partial view
a c# model class
If I use the standard way to manage the files, I have to spread them in these folders:
/Scripts
/Content/css
/View
/Models
I know that MVC is for separating them and it's fine for the application but i my case I would be happier to have all files somewhere together.
There are area's in MVC but I think that's to big for a small module. All I want is to build a small package in my application for the files of this module.
Any good approach for this? How do you handle this?
/Scripts
/Content/css
/View
/Models
This is just the structure that the default asp.net mvc project template imposes.
You can have any structure you like, but I would recommend keeping your Views and Model separate.
Since our "Model" usually sits in a different assembly our MVC project structure is normally:
Application (all application infastructure code)
Content
css
images
scripts
Controllers
ViewModels
Views
Again this is personal preference but I typically like to keep my static assets (css,images,scripts) under one directory.
You can of course separate things out even further. For example if we're using a javascript plugin that has it's own "core" css and images then we normally keep these together e.g.:
Content
css
images
scripts
libs
myplugin
myplugin.js
myplugin.css
myplugin.png
Of course as you split things out in this way it can become hard to manage. For this reason we use Client Dependency Framework. You could also try Cassette.

Resources