Skip to content
Permalink
Browse files

Initial commit

  • Loading branch information...
robbz committed Mar 13, 2013
0 parents commit 2c500bf401ed4a02684cd52ef25957fecb4a8ddf
@@ -0,0 +1,9 @@
# Node.js folders
*node_modules/

# Build artefacts
*build/

# OS generated files and folders
.DS_Store
npm-debug.log
@@ -0,0 +1,43 @@
'use strict';

module.exports = function (grunt) {
grunt.initConfig({
jshint: {
files: [ '**/*.js', '!node_modules/**/*.js', '!public/**/*.js' ],
options: {
jshintrc: 'jshint.json'
}
},

pkg: grunt.file.readJSON('package.json'),
uglify: {
options: {
banner: '/*!\n' +
' * <%= pkg.name %> - v<%= pkg.version %>\n' +
' * (c) <%= pkg.author %>\n' +
' * License: MIT (http://www.opensource.org/licenses/mit-license.php)\n' +
' * <%= grunt.template.today("yyyy-mm-dd") %> \n' +
' */\n'
},
my_target: {
files: {
'bin/flexigin-<%= pkg.version %>.min.js': ['lib/flexigin.js', 'lib/load.js', 'lib/resolve.js']
}
}
},

watch: {
options: {
files: [ 'lib/*.js', '!node_modules/**/*.js' ],
tasks: [ 'default' ],
interrupt: true
}
}
});

//grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-uglify');

grunt.registerTask('default', [ 'uglify' ]);
};
@@ -0,0 +1,99 @@
# flexigin

flexigin is a flexible framework for building simple, fast and powerful apps based on top of knockout.

As many libs are only client-side, flexigin comes with an implementation for several servers, which is one key point of the concept.

- nodejs implementation - [flexigin-node](https://github.com/goloroden/flexigin-node)
- .NET implementation - [flexigin-net](https://github.com/robbz/flexigin-node)

## What is flexigin?

As we have seen, flexigin has a client and a server part. First of all, lets take a look at the main concept:

Assuming to have an app for editing users and their profiles, then this means to have a user and a profile model and view.
This model + view + some css stuff alltogheter are one component.

Flexigin expect them to be in a basePath like `/components` and to have the subfolders called like the name of the component should be.

/components/user
/user.js
/user.html
/user.css
/profile
/profile.js
/profile.html
/profile.css

When the client would like to get access to this components, it simple loads the files needed and automatically bind the model to the view.
The server should be at least able to provide one specified component or all components togheter.
It doesn't mather how many .js, .css or .html files there are, the result is returned **concatenated** and **minified**.

#### Examples

/components/user/js => all js files located in the /user folder, concatenated and minified.
/components/profile/html => all html files located in the /user/profile folder, concatenated and minified.
/components/css => all css files located in the basePath /components (including all subfolders), concatenated and minified.
/components/ => if no type is specified, a requests for each type ill be created.

## Quick start

Simple call:

fx('user', function() {
// All the files needed for the user component have been loaded.
});

Call with the container option:
The container is a jQuery selector where the loaded html should be appended:

fx('user', { container: '#user' }, function() {

});

Call with model-binding:
The model binding is a function which will be applied with knockout.
If also the container is specified, this means it will be only bind to the partial view.

fx('user', { viewModel: function() { return fx.user; }, container: '#user' }, function() {

});

Changing the route:
The route has two placeholders, {component} and {type}.

fx.set('route', '/flexigin/?p={component}/{type}/'); // .NET route
fx.set('route', '/basePath/{component}/{type}/'); // nodejs route

## Partial views

If you have a small app, then it's not a problem to load all the files on the begin, since they are minified.

// Load all
fx(function() {

});

But if your app is going to get bigger from time to time, than it's better to use partial views, means to only load the component desired.

// Load the js of the profile component
fx('user/profile/js', function() {})

## Running the tests

Flexigin has been developed using TDD. To run the tests, you need to start the sample node app (app.js) and open the following url in your browser:

http://localhost:3000/runner.html

You need to have [mocha](https://github.com/visionmedia/mocha) installed.

## License

The MIT License (MIT)
Copyright (c) 2013 Roberto Bez.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15 app.js
@@ -0,0 +1,15 @@
var http = require('http'),
path = require('path');

var express = require('express'),
flexigin = require('flexigin-node');

var app = express();
app.use(flexigin({
baseUrl: '/test/components',
basePath: path.join(__dirname, '/test/components')
}));
app.use(express.static(__dirname + '/lib'));
app.use(express.static(__dirname + '/test'));

http.createServer(app).listen(3000);

Some generated files are not rendered by default. Learn more.

@@ -0,0 +1,30 @@
(function(window) {
var fx = function(components, options, callback) {
if (typeof components === 'function') {
components = '/';
callback = components;
}
if (typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
components = fx.resolve(components);
fx.load(components, options, callback);
};

fx.set = function(key, value) {
fx[key] = value;
};
fx.get = function(key) {
return fx[key];
}

window.fx = fx;

if (typeof define !== 'undefined') {
define(function () {
return fx;
});
}
})(window);
@@ -0,0 +1,75 @@
(function(fx, $) {
var load = function(components, options, callback) {
options = options || {};

var query = fx.query();
$.each(components, function(key, component) {
query.add(component, options);
});
if (query.isEmpty()) {
callback();
return;
}
query.run(options, callback);
};

fx.loaded = [];
var query = function() {
var query = [];
var loader = this;
var deferreds = getDeferreds();
return {
add: function(component, options) {
if (fx.loaded.indexOf(component) !== -1) {
return;
}
query.push(deferreds[component.type](component.url, options));
loader.loaded.push(component);
},
run: function(options, callback) {
var onLoad = function(data) {
if (typeof ko !== 'undefined' && typeof options.viewModel !== 'undefined') {
var viewModel = options.viewModel();
if (viewModel.hasOwnProperty('init')) {
viewModel.init();
}
ko.applyBindings(viewModel, $(options.container || 'body')[0]);
}
callback(null, data);
};
$.when.apply($, query)
.done(onLoad)
.fail(callback);
},
length: function() {
return query.length;
},
isEmpty: function() {
return query.length === 0;
}
}
};

var getDeferreds = function() {
return {
html: function(url, options) {
var onLoad = function(html) {
$(options.container || 'body').append(html);
};
return $.get(url).done(onLoad);
},
css: function(url) {
var onLoad = function(css) {
$('head').append('<style>' + css + '</style>');
};
return $.get(url).done(onLoad);
},
js: function(url) {
return $.getScript(url);
}
};
};

fx.load = load;
fx.query = query;
})(fx, jQuery);
@@ -0,0 +1,57 @@
(function(fx, $) {
var url = function() {
return {
build: function(component, type) {
component = trimSlash(component);
return (fx.get('route') || '/{component}/{type}/')
.replace('{component}', component)
.replace('{type}', type);
},
getExtension: function(url) {
var parts = url.split('/');
return parts[parts.length - 1];
}
}
};

var resolve = function(components) {
var url = fx.url();
var extensions = ['js', 'css', 'html'];
var createComponent = function(component, extension) {
return {
url: url.build(component, extension),
type: extension
};
}

var resolved = [];
if (typeof components === 'string') {
components = [components];
}
$.each(components, function(key, component) {
if (typeof component === 'string') {
var extension = url.getExtension(component);

// If we got an extensionless url, create a component for each extension.
if (extensions.indexOf(extension) === -1) {
$.each(extensions, function(key, extension) {
resolved.push(createComponent(component, extension));
});
} else {
component = component.substr(0, component.lastIndexOf('/'));
resolved.push(createComponent(component, extension));
}
} else {
resolved.push(component);
}
});
return resolved;
};

var trimSlash = function(text) {
return text.replace(/^\/+|\/+$/g, '');
};

fx.resolve = resolve;
fx.url = url;
})(fx, jQuery);
@@ -0,0 +1,23 @@
{
"name": "flexigin",
"version": "0.0.1",
"description": "flexigin is a flexible framework for building apps with knockout.",
"author": "Roberto Bez (http://www.devangelist.de)",
"main": "bin/flexigin.min.js",
"dependencies": {
"flexigin-node": "0.0.1"
},
"devDependencies": {
"connect": "2.7.3",
"grunt": "0.4.0",
"grunt-contrib-watch": "0.2.0",
"grunt-contrib-uglify": "0.1.2"
},
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git://github.com/robbz/flexigin.git"
}
}
@@ -0,0 +1,3 @@
* {
margin: 0;
}
@@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<title>flexigin</title>
</head>
<body>
flexigin
</body>
</html>
@@ -0,0 +1,3 @@
#user {
width: 100px;
}

0 comments on commit 2c500bf

Please sign in to comment.
You can’t perform that action at this time.