BugoMax
6/23/2017 - 12:42 PM

MVC JavaScript with VK

MVC JavaScript with VK

var Model = {
	login: function(appId, perms) {
		return new Promise(function(resolve, reject) {
			VK.init({
				appId: appId
			});

			VK.Auth.login(function(response) {
				if (response.session) {
					resolve(response);
				} else {
					reject(new Error('Не удалось авторизоваться'));
				}
			}, perms);
		});
	},
	callApi: function(method, params) {
		return new Promise(function(resolve, reject) {
			VK.api(method, params, function(response) {
				if (response.error) {
					reject(new Error(response.error.error_msg));
				} else {
					resolve(response.response)
				}
			});
		});
	},
	getUser: function() {
		return this.callApi('users.get', {});
	},
	getMusic: function() {
		return this.callApi('audio.get', {});
	},
	getFriends: function() {
		return this.callApi('friends.get', {fields: 'photo_100'});
	},
	getNews: function() {
		return this.callApi('newsfeed.get', {filters: 'post', count: 20});
	}
}


var View = {
	render: function(templateName, model) {
		templateName = templateName + 'Template';

		var templateElement = document.getElementById(templateName),
			templateSource  = templateElement.innerHTML,
			renderFn        = Handlebars.compile(templateSource);

		return renderFn(model);
	}
}


var Controller = {
	musicRoute: function() {
		return Model.getMusic().then(function(music) {
			results.innerHTML = View.render('music', {list: music});
		});
	},
	friendsRoute: function() {
		return Model.getFriends().then(function(friends) {
			results.innerHTML = View.render('friends', {list: friends});
		});
	},
	newsRoute: function() {
		return Model.getNews().then(function(news) {
			results.innerHTML = View.render('news', {list: news.item});
		});
	}
}

///////

var Router = {
	handle: function(route) {
		var routeName = route + 'Route';

		Controller[routeName]();
	}
}

///////2017-06-22
date ("d.m.Y", time() - +7 + date("N")); 

Handlebars.registerHelper('formatTime', function(time) {
	var minutes = parseInt(time / 60),
        seconds = time - minutes * 60;

    minutes = minutes.toString().length === 1 ? '0' + minutes : minutes;
    seconds = seconds.toString().length === 1 ? '0' + seconds : seconds;

    return minutes + ':' + seconds;
});

Handlebars.registerHelper('formatDate', function(ts) {
    return new Date(ts * 1000).toLocaleString();
});

new Promise(function(resolve) {
	window.onload = resolve;
}).then(function() {
	return Model.login(5267932, 2 | 8 | 8192);
}).then(function() {
	return Model.getUser().then(function(users) {
		header.innerHTML = View.render('header', users[0]);
	});
}).catch(function(e) {
	console.error(e);
	alert('Error: ' + e.message);
});