Skip to content

Instantly share code, notes, and snippets.

@philmander
Last active December 10, 2015 10:38
Show Gist options
  • Save philmander/4422058 to your computer and use it in GitHub Desktop.
Save philmander/4422058 to your computer and use it in GitHub Desktop.
Inverted intro 2 with dependency injection
//movies/movie-lister.js
define(function() {
var MovieLister = function(movieFinder) {
this.movieFinder = movieFinder;
};
MovieLister.prototype.showMovies = function(query) {
var keywords = query.split(" ");
var movieResult = this.movieFinder.getMovies(keywords);
movieResult.done(function() {
movies.forEach(function(movie) {
console.log(movie.title);
});
});
};
return MovieLister;
});
//movies/json-movie-finder.js
define(["jsonp"], function(jsonP) {
var JsonMovieFinder = function(jsonpUrl) {
this.jsonpUrl = jsonpUrl;
};
JsonMovieFinder.prototype.getMovies = function(keywords) {
var result = jsonP.getJson(this.jsonUrl, {
keywords: keywords || ""
});
return result;
};
return JsonMovieFinder;
});
//movie-app.js
//load all dependencies up front
require([ "movies/movie-lister", "movies/json-movie-finder", function(MovieLister, MovieFinder) {
//wire them together
var jsonUrl = "http://jsonp.movies.com/movies";
var movieFinder = new MovieFinder(jsonUrl);
var movieLister = new MovieLister(movieLister);
movieLister.showMovies();
});
@AndriiHeonia
Copy link

I think there is small mistake in inverted-intro-2-3.js

var movieFinder = new MovieFinder(jsonUrl);
var movieLister = new MovieLister(movieLister); // wrong param, should be movieFinder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment