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

Best practice for sammy js #264

Open
El-Mahbub opened this issue Oct 13, 2017 · 0 comments
Open

Best practice for sammy js #264

El-Mahbub opened this issue Oct 13, 2017 · 0 comments

Comments

@El-Mahbub
Copy link

El-Mahbub commented Oct 13, 2017

How is the best practice with sammy js ?
I have this codes, what is it best practice ? But i think it is not a best practice.
My codes :
`
var app, app_name, base_url, url = [], cdn, websocket;
var body, navbar, container_content, left_side, main_side, main_content, other_side, right_side, footer;
var uid, user_id, name;

// Config
var config = {
host : 'http://localhost:8080/xampp/MyApp/',
app_name : window.location.pathname.split('/').length > 1 ? window.location.pathname.split('/')[2] : window.location.pathname.split('/')[1],
target : 'body'
}

// Autoload
var autoload = {
base_url: config.host,
error_404: ''
};

// Url's
var url = {
login: '#/',
profile: 'profile/index/',
kronologi: 'profile/kronologi/',
status_profile: 'profile/status/',
global: 'page/global_page',
status_global: 'page/status_global/',
recomended: 'page/all_recomended'
}

// System
Object.keys(url).forEach(function (key) {
url[key] = config.host + url[key];
});

base_url = autoload.base_url;
app_name = config.app_name;
name = $('#nama-profile').children().text();

// Controller
var routes = $.sammy(function(){
app = this;
this.element_selector = config.target;
this.run_interval_every = 60000;
this.get('#/', function (context) {

});
this.get('#/login', function (context) {
	app.redirect('#/');
});
this.get('#/signup', function (context) {
	
});
this.get('#/profile/index/:username', function (context) {
	var params = this.params['username'];
	profile(context.$element(),url.profile+params,params);
});
this.get('#/global/', function (context) {
	render(context.$element(),url.global,'status_global');
});
this.get('#/gallery/:username', function (context) {
	
});;
this.get('#/adsennse/:username', function (context) {
	
});
this.get('#/report/:id', function (context) {
	
});
this.get('#/help/', function (context) {
	
});
this.get('#/feedback/:id', function (context) {
	
});
this.get('#/about/', function (context) {
	
});
this.get('#/log_out/:id', function (context) {
	
});

}).run('#/');

// Views

// Profile
function profile(el,controller,name) {
render(el,controller,'kronologi');
change_title(str_replace(app_name,'/','')+' - '+str_replace(name,'',' '));
$(document).find('.title-user').html('

Profil '+str_replace(name,'',' ')+'

');

}
function kronologi(el,controller,user_id) {
render(el,controller,'status_profile');
}

// Global

function global(el,controller) {
change_title(str_replace(app_name,'/','')+' - '+'Global');
$(document).find('.title-user').html('

Global page

');
render(el,controller,'');
}

// Helpers

function render(params1,params2,params3) {
$.ajax({
url: params2,
type: 'post',
beforeSend:function (argument) {

	},
	error:function (data) {
		error_load($(params1),data);	
	},
	success: function (data) {
		$(params1).html(data);
		navbar = $('nav');
		container_content = $('#container');
		left_side = $('#menu_user');
		main_side = $('.profile_content');
		main_content = $('.main_profile');
		content_status = $('#status-profile');
		other_side = $('.statistic_user');
		right_side = $('#online_side');
		footer = $('footer');
		uid = $('.title-user').attr('ux');
		user_id = $('.title-user').attr('ui');
		uid === '' ? window.location.href = url.login : null;
		if(params3 !== ''){
			switch (params3) {
				case 'kronologi':
					kronologi(main_content,url.kronologi+user_id,user_id);
					break;
				case 'status_profile':
					loadStatus(content_status,url.status_profile+user_id+'/');
					status();
					break;
				case 'status_global':
					global(other_side,url.recomended);
					loadStatus(main_content,url.status_global+uid+'/');
					break;
			}
		}
		else { null; }
		}
});
return true;

}
function site_url(args){
return config.host + args;
}
function loadStatus(el,urlTarget) {
$(el).loadStatus({
loading : "",
no_news : "",
error_text : "Terjadi kesalahan memuat konten",
url : urlTarget,
start : 1
});
}
function change_title(argument) {
$(document).find('title').text(argument);
}
function str_replace(args,haystack,needle) {
return args.replace(haystack,needle);
}`

Hope you can give me the best practice in documentation, because i know about jquery but just litle for sammy :-)

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

1 participant