Move files

This commit is contained in:
Kload 2013-07-01 19:32:11 +02:00
parent d6e1a19f16
commit 8847faaa6e
13 changed files with 58 additions and 159 deletions

View file

@ -17,13 +17,13 @@
<a href="#/login">Grset</a>
</div>
<script type="text/javascript" src="js/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="js/mustache.js"></script>
<script type="text/javascript" src="js/sammy.js"></script>
<script type="text/javascript" src="js/sammy.mustache.js"></script>
<script type="text/javascript" src="js/sammy.json.js"></script>
<script type="text/javascript" src="js/sammy.storage.js"></script>
<script type="text/javascript" src="js/sammy.flash.js"></script>
<script type="text/javascript" src="js/vendor/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="js/vendor/mustache.js"></script>
<script type="text/javascript" src="js/vendor/sammy.js"></script>
<script type="text/javascript" src="js/vendor/sammy.mustache.js"></script>
<script type="text/javascript" src="js/vendor/sammy.json.js"></script>
<script type="text/javascript" src="js/vendor/sammy.storage.js"></script>
<script type="text/javascript" src="js/vendor/sammy.flash.js"></script>
<script type="text/javascript" src="js/app.js"></script>
</body>

View file

@ -4,47 +4,52 @@ app = Sammy('#main', function (sam) {
var store = new Sammy.Store({name: 'storage', type: 'session'});
var flash = function (level, message) {
flashs = store.get('flash');
if (!flashs) { flashs = {'info': [], 'fail': [], 'success': [] } }
flashs[level].push(message);
store.set('flash', flashs);
sam.helpers({
flash: function (level, message) {
flashs = store.get('flash');
if (!flashs) { flashs = {'info': [], 'fail': [], 'success': [] } }
flashs[level].push(message);
store.set('flash', flashs);
html = '';
for(lvl in flashs) {
flashs[lvl].forEach( function(msg) {
html += '<div class="'+ lvl +'">'+ msg +'</div>';
});
}
$('#flash').html(html);
}
var api = function (uri, callback, method, data) {
method = typeof method !== 'undefined' ? method : 'GET';
data = typeof data !== 'undefined' ? data : {};
auth = "Basic "+ btoa(store.get('user') +':'+ atob(store.get('password')));
jQuery.ajax({
url: store.get('url') + uri,
type: method,
crossdomain: true,
data: data,
dataType: 'json',
beforeSend: function(req) {
req.setRequestHeader('Authorization', auth);
html = '';
for(lvl in flashs) {
flashs[lvl].forEach( function(msg) {
html += '<div class="'+ lvl +'">'+ msg +'</div>';
});
}
})
.done(function(data) {
console.log(data);
result = data;
})
.fail(function() {
alert('fail');
result = false;
})
.always(function() {
callback(result);
});
}
$('#flash').html(html);
},
api: function (uri, callback, method, data) {
method = typeof method !== 'undefined' ? method : 'GET';
data = typeof data !== 'undefined' ? data : {};
auth = "Basic "+ btoa(store.get('user') +':'+ atob(store.get('password')));
jQuery.ajax({
url: store.get('url') + uri,
type: method,
crossdomain: true,
data: data,
dataType: 'json',
beforeSend: function(req) {
req.setRequestHeader('Authorization', auth);
}
})
.done(function(data) {
console.log(data);
result = data;
})
.fail(function() {
alert('fail');
result = false;
})
.always(function() {
callback(result);
});
},
view: function (view, data) {
//data = typeof data !== 'undefined' ? data : {};
this.render('views/'+ view +'.ms', data).swap();
}
});
sam.after(function () {
store.set('flash', {'info': [], 'fail': [], 'success': [] });
@ -59,29 +64,29 @@ app = Sammy('#main', function (sam) {
});
sam.get('#/', function (c) {
c.render('home.ms').swap();
c.view('home');
});
sam.get('#/users/:user', function (c) {
c.swap('');
api('/users/'+ c.params['user'], function(data) {
c.render('user_info.ms', data).swap();
c.api('/users/'+ c.params['user'], function(data) {
c.view('user_info', data);
});
});
sam.get('#/login', function (c) {
c.render('login.ms').swap();
c.view('login');
});
sam.post('#/login', function (c) {
store.set('url', c.params['url']);
store.set('user', c.params['user']);
store.set('password', btoa(c.params['password']));
api('/users', function(data) {
c.api('/users', function(data) {
if (data.error) {
flash('fail', 'Error: '+ data.error);
c.flash('fail', 'Error: '+ data.error);
} else {
flash('success', 'Connected :)');
c.flash('success', 'Connected :)');
}
if (store.get('path')) {
c.redirect(store.get('path'));

View file

@ -1,106 +0,0 @@
(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery', 'sammy'], factory);
} else {
(window.Sammy = window.Sammy || {}).Flash = factory(window.jQuery, window.Sammy);
}
}(function ($, Sammy) {
Sammy.FlashHash = function() {
this.now = {};
};
$.extend(Sammy.FlashHash.prototype , {
// @return [String] this Flash, rendered as an <ul>.
toHTML: function() {
var result = this._renderUL();
this.clear();
return result;
},
clear: function() {
this._clearHash(this);
this._clearHash(this.now);
},
// Callback on redirect.
// @api private
_onRedirect: function() {
this._clearHash(this.now);
},
// clear out all flash keys
// @api private
_clearHash: function(hash) {
var key;
for (key in hash) {
if (key !== 'now' && hash.hasOwnProperty(key)) {
delete hash[key];
}
}
},
_renderUL: function() {
return '<ul class="flash">' +
this._renderLIs(this) +
this._renderLIs(this.now) +
'</ul>';
},
_renderLIs: function(hash) {
var result = '',
key;
for (key in hash) {
if (hash[key] && key !== 'now' && hash.hasOwnProperty(key)) {
result = result + '<li class="' + key + '">' + hash[key] + '</li>';
}
}
Sammy.log('rendered flash: ' + result);
return result;
}
});
// Sammy.Flash is a plugin for storing and sending status messages to the client. It's API and use
// is similar to Ruby on Rails' `flash` explained here:
// [http://apidock.com/rails/ActionController/Flash](http://apidock.com/rails/ActionController/Flash)
Sammy.Flash = function(app) {
app.flash = new Sammy.FlashHash();
// *Helper* flash(key, value) get or set a flash message that will
// be erased on the next render (but not on redirect).
//
// @param [String] key, the Flash key
// @param [String] value, the new value; optional
// @return [Sammy.FlashHash, String, null] if a key was given, the value for that key; else, the Flash
app.helper('flash', function(key, value) {
if (arguments.length === 0) {
return this.app.flash;
} else if (arguments.length === 2) {
this.app.flash[key] = value;
}
return this.app.flash[key];
});
// *Helper* flashNow(key, value) get or set a flash message that
// will be erased on the next render or redirect.
//
// @param [String] key, the Flash key
// @param [String] value, the new value; optional
// @return [String, null] the value for the given key
app.helper('flashNow', function(key, value) {
if (arguments.length === 0) {
return this.app.flash.now;
}else if (arguments.length === 2) {
this.app.flash.now[key] = value;
}
return this.app.flash.now[key];
});
app.bind('redirect', function() {
this.app.flash._onRedirect();
});
};
return Sammy.Flash;
}));

View file