Skip to content

Instantly share code, notes, and snippets.

@byverdu
Last active May 30, 2017 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byverdu/0ffb993ca92c5b26fdda1275f3b289fd to your computer and use it in GitHub Desktop.
Save byverdu/0ffb993ca92c5b26fdda1275f3b289fd to your computer and use it in GitHub Desktop.
// app/client/js/controllers/homeController.js
module.exports = function ( service, broadcaster, $rootScope, $timeout, Notification ) {
const $home = this;
$home.title = 'Welcome to ImdbApp';
$home.imdbText = ''; // texto del formulario
$home.imdbType = 'movie'; // usar 'movie' como tipo por defecto
$home.imdbData = {};
$home.links = [
{ text: 'Movies', url: '/imdb/movie' },
{ text: 'TvShows', url: '/imdb/series' }
];
$home.contentReady = false;
$rootScope.$on( 'item:searched', function ( event, item ) {
// item es la respuesta de la Promise que devuelve nuestro servicio
$timeout(() => {
// mostrar el error si la respuesta contiene la propiedad 'message'
if ( 'message' in item ) {
Notification.error( item.message );
return;
}
$home.imdbData = item.data;
$home.contentReady = false;
}, 500 );
});
$home.callImdbApi = function () {
$home.contentReady = true;
service.getImdbData( this.imdbText, this.imdbType )
.then( response => broadcaster.itemSearched( response ));
$home.imdbText = '';
$home.imdbType = '';
};
$home.saveToDb = function () {
service.addItem( $home.imdbData )
.then(( response ) => {
if ( response.status === 200 ) {
Notification.success( `${response.data}` );
$home.imdbData = null;
} else {
Notification.error( 'Something went wrong saving on DB' );
}
});
};
};
// app/client/views/home.pug
block content
.wrapper(ng-controller="HomeController as home")
h1(ng-bind="home.title")
nav
div(ng-repeat="link in home.links")
a(ng-href="{{link.url}}") {{link.text}}
form.searchImdb(ng-submit="home.callImdbApi()")
fieldset
label Search on Imdb
input(type="text", placeholder="type name film", ng-model="home.imdbText")
input(type="text", placeholder="type of imdb", ng-model="home.imdbType")
input(type="submit", value="Search")
div(ng-show="home.contentReady") Loading ...
.latestItem(ng-show="home.imdbData.title !== undefined")
// imdb-card es la directiva
imdb-card(data="home.imdbData", call-action="home.saveToDb()" text-btn="Save")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment