Error: Unknown provider: aProvider <- a - ruby-on-rails

I'm using AngularJS in a Ruby on Rails 3.2.8 project with assets.
When I load up my form which is using AngularJS on my development machine I don't have a problem. However when I load the same form up on my production server I get this error in the Javascript console:
Error: Unknown provider: aProvider <- a
I've tracked it back to my coffeescript file where I setup AngularJS for use within a form:
$ (event) ->
$("#timesheet_description").autocomplete({source: '/autocomplete/work_descs'})
# Create AngularJS module
app = angular.module 'timesheetApp', []
# Create a AngularJS controller
app.controller "TimesheetCtrl", ($scope) ->
$scope.costed_amount = 0
# Bind my module to the global variables so I can use it.
angular.bootstrap document, ["timesheetApp"]
If I comment all this out the page will load without errors and without AngularJS abilities.
Is the problem due to Rails assets compiling and minify?
Is there a way to fix this and still use coffeescript and Rails assets?

AngularJS, when using the style you're using right now (called pretotyping), uses the function argument names to do dependency injection. So yes, minification does break this completely.
The fix is simple, though. In every case where you need injection (are using '$xxx') variables, do this:
app.controller "TimesheetCtrl", ['$scope', ($scope) ->
$scope.costed_amount = 0
]
Basically, replace all function definitions with an array. The last element should be the function definition itself, and the first ones are the $names of the objects you want injected.
There's some more (albeit not clear enough) info on the docs.

If you miss the array notation somewhere , to locate this we need to modify the angular code little bit, but its very quick solution.
change is console.log("Array Notation is Missing",fn); ( line no 11 from function start)
Find out annotate function in angular.js (non-minified)
function annotate(fn) {
var $inject,
fnText,
argDecl,
last;
if (typeof fn == 'function') {
if (!($inject = fn.$inject)) {
$inject = [];
if (fn.length) {
console.log("Array Notation is Missing",fn);
fnText = fn.toString().replace(STRIP_COMMENTS, '');
argDecl = fnText.match(FN_ARGS);
forEach(argDecl[1].split(FN_ARG_SPLIT), function(arg){
arg.replace(FN_ARG, function(all, underscore, name){
$inject.push(name);
});
});
}
fn.$inject = $inject;
}
} else if (isArray(fn)) {
last = fn.length - 1;
assertArgFn(fn[last], 'fn');
$inject = fn.slice(0, last);
} else {
assertArgFn(fn, 'fn', true);
}
return $inject;
}

To minify angular all you need is to do is to change your declaration to the "array" declaration "mode" for example:
From:
var demoApp= angular.module('demoApp', []);
demoApp.controller(function demoCtrl($scope) {
} );
To
var demoApp= angular.module('demoApp', []);
demoApp.controller(["$scope",function demoCtrl($scope) {
}]);
How to declare factory services?
demoApp.factory('demoFactory', ['$q', '$http', function ($q, $http) {
return {
//some object
};
}]);

Related

How to pass variables from rails controller to AngularJs

I have a rails controller for recipes. I have added some additional variable called #recID. I am able to access this variable successfully in index.json.jbuilder.
I can't figure out how to access it in my app.js (which is angular controller file).
My research has covered:
How can i pass a scope variable from controller to directive in angular js
How can controller talk to a directive in AngularJS?
and many other google searches.
Here is a code snippet in from app.js:
$scope.search = function(keywords) {
return $location.path("/").search('keywords', keywords);
};
// query = $resource('/', {query: "query"});
// query = $resource('/recipes/:recipeId', {
query = $resource('/', {
query: "#recID",
format: 'json'
});
console.log ("About to write to log query=" + query);
// alert ("query =<" + query + ">");
Recipe = $resource('/recipes/:recipeId', {
recipeId: "#id",
format: 'json'
});
try using gon.gon is a gemused to pass data to js.first you need to install gon.then in your controller you can specify
gon.variable_name = variable_value
in your js
gon.variable_name
https://github.com/gazay/gon
https://gist.github.com/shicholas/5937417
You can set some hidden_field in the HTML with controller variable value and fetch this value in app.js using DOM selector.

Getting the project base path in a yeoman generator

I have updated the yeoman generator dependency from 0.18.10 to 0.20.3.
I have updated the deprecated this.dest to this.destinationRoot()
I am now having issues with the generator when it comes to getting the base path of the project, so that I can copy files from one location to another.
I have created a function to put the paths together, this then passes to another function which excludes some files from being copied over.
Here is the function I getting the error with
// Copy Bower files to another directory
var copyBowerFiles = function (component, to, exclude) {
var base = this.destinationRoot(),
publicDir = base + '/' + this.publicDir,
publicAssetsDir = publicDir + '/assets',
bowerComponentsDir = publicAssetsDir + '/bower_components',
bower,
from;
to = (base + '/' + to || publicAssetsDir);
from = bowerComponentsDir + '/' + component;
//this.dest.copy(from, to);
this.bulkDirectory(from, copyDestPathPartial.call(this, to, exclude));
};
This is being called in the end function:
end: function () {
this.installDependencies({
callback: function () {
copyBowerFiles.call('jam', this.publicDir, excludeJamFiles);
}.bind(this)
});
}
I get the error message:
var base = this.destinationRoot(),
^
TypeError: undefined is not a function
I have also tried sourceRoot()
I would like to update my generator to work with the latest version of the generator. Any help getting this working would be great.
Also do you still have to pass this as the first parameter when calling a function?
EDIT:
Here is the copyDestPathPartial function
// Copy destination path partial
var copyDestPathPartial = function (to, exclude) {
exclude = exclude || [];
return function (abs, root, sub, file) {
if (!_.contains(exclude, file) && ! _.contains(exclude, sub)) {
this.copy(abs, to + '/' + (sub || '') + '/' + file);
}
}.bind(this.destinationRoot());
};
When I use this in the copyBowerFiles function I get another error message which says when I call this function:
throw new TypeError('Arguments to path.resolve must be strings');
Is the copyDestPathPartial function not outputting a string?
This is only a JavaScript error, this inside copyBowerFiles is not what you think it is.
With the code you wrote, this is equal to jam.
So here you'd want: copyBowerFiles.call(this, 'jam', this.publicDir, excludeJamFiles);. As the first argument to call is the this value. See documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
That being said, assigning random this value is very dirty and super hard to maintain. Why not making copyBowerFiles a prototype method?

Ionic Prepopulated Database with Antair Cordova SQLitePlugin [help request]

____ INTRO
Hello everyone, first of all, three clarifications:
My english is not good, so I beg your pardon in advance for my mistakes,
I'm a newbie so forgive me for inaccuracies,
I have previously searched and tried the solutions I found on the internet but still I can not solve the problem of embedding a prepopulated database.
____ THE GOAL
I want to develop an app for iOS and Android with a prepopulated database.
Just for example, the database consists of 15.000 records each one made of three key-value pair (id, firstname and lastname).
___ WHAT I DID
Steps:
ionic start myapp blank
cd myapp
ionic platform add ios
ionic platform add android
Then I created an sqlite database for testing purpose, named mydb.sqlite, made of one table people containing two id, firstname, lastname records.
I decided to use the following plugin: https://github.com/Antair/Cordova-SQLitePlugin
That's because it can be installed with cordova tool.
ionic plugin add https://github.com/Antair/Cordova-SQLitePlugin
(Alert: I think that the instructions on the website show an incorrect reference - "cordova plugin add https://github.com/brodysoft/Cordova-SQLitePlugin" - which refers to another plugin).
Then, following the instructions on the plugin website, I copied the database to myapp/www/db/ so that it can now be found at myapp/www/db/mydb.sqlite
I modified the index.html including the SQLite plugin just after the default app.js script:
<!-- your app's js -->
<script src="js/app.js"></script>
<script src="SQLitePlugin.js"></script>
I also write some lines of code in index.html file to show a button:
<ion-content ng-controller="MyCtrl">
<button class="button" ng-click="all()">All</button>
</ion-content>
Finally I had modified ./js/app.js:
// Ionic Starter App
var db = null;
angular.module('starter', ['ionic' /* What goes here? */ ])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// some predefined code has been omitted
window.sqlitePlugin.importPrepopulatedDatabase({file: "mydb.sqlite", "importIfExists": true});
db = window.sqlitePlugin.openDatabase({name: "mydb.sqlite"});
}); // $ionicPlatform.ready
}) // .run
.controller('MyCtrl', function($scope){
$scope.all = function(){
var query = "SELECT * FROM people";
// I don't know how to proceed
}; // $scope.all
}); // .controller
___ THE PROBLEM
I don't know how to proceed in the controller section to query all the records (just an example of query) and show the results in the console.log.
I think that the following code must be completed in some way:
angular.module('starter', ['ionic' /* What goes here? */ ])
And also the code inside controller section must be completed:
$scope.all = function(){
var query = "SELECT * FROM people";
// I don't know how to proceed
}; // $scope.all
___ FINAL THANKS
Thank you in advance for the help you will give to me.
So this guy's code has helped a lot to encapsulate my DAL. I highly recommend that you use he's code pretty much verbatim.
https://gist.github.com/jgoux/10738978
You'll see he has the following method:
self.query = function(query, bindings) {
bindings = typeof bindings !== 'undefined' ? bindings : [];
var deferred = $q.defer();
self.db.transaction(function(transaction) {
transaction.executeSql(query, bindings, function(transaction, result) {
deferred.resolve(result);
}, function(transaction, error) {
deferred.reject(error);
});
});
return deferred.promise;
};
Let's break this down a bit. The query function takes a query string (the query param) and a list of possible bindings for ? in a query like "SELECT * FROM A_TABLE WHERE ID = ?". Because he's code is a service, the self value points to the service itself for all future invocations. The function will execute a transaction against the db, but it returns a promise that is only fulfilled once the db comes back.
His service provides a second helper function: fetchAll.
self.fetchAll = function(result) {
var output = [];
for (var i = 0; i < result.rows.length; i++) {
output.push(result.rows.item(i));
}
return output;
};
fetchAll will read the rows in their entirety into an array. The result param for fetchAll is the result variable passed in the query function's promise fulfillment.
If you copy and paste his code into your service file, you now have a bonafide DB service. You can wrap that service up in a DAL. Here's an example from my project.
.service('LocationService', function ($q, DB, Util) {
'use strict';
var self = this;
self.locations = [];
self.loadLocked = false;
self.pending = [];
self.findLocations = function () {
var d = $q.defer();
if (self.locations.length > 0) {
d.resolve(self.locations);
}
else if (self.locations.length === 0 && !self.loadLocked) {
self.loadLocked = true;
DB.query("SELECT * FROM locations WHERE kind = 'active'")
.then(function (resultSet) {
var locations = DB.fetchAll(resultSet);
self.locations.
push.apply(self.locations, locations);
self.loadLocked = false;
d.resolve(self.locations);
self.pending.forEach(function (d) {
d.resolve(self.locations);
});
}, Util.handleError);
} else {
self.pending.push(d);
}
return d.promise;
};
})
This example is a bit noisy since it has some "threading" code to make sure if the same promise is fired twice it only runs against the DB once. The general poin is to show that the DB.query returns a promise. The "then" following the query method uses the DB service to fetchAll the data and add it into my local memory space. All of this is coordinated by the self.findLocations returning the variable d.promise.
Yours would behalf similarly. The controller could have your DAL service, like my LocationService, injected into it by AngularJS. If you're using the AngularJS UI, you can have it resolve the data and pass it into the list.
Finally, the only issue I have with the guy's code is that the db should come from this code.
var dbMaker = ($window.sqlitePlugin || $window);
The reason for this is that the plugin does not work within Apache Ripple. Since the plugin does a fine job mirroring the Web SQL interface of the browser, this simple little change will enable Ripple to run your Ionic Apps while still allowing you to work your SQLite in a real device.
I hope this helps.

Angular Rails Karma testing Controller with factory (with backend api calls) dependency

I have a projectFactory:
#app.factory "projectFactory", ['$http', ($http) ->
factory = {}
factory.loadProject = (projectId) ->
$http.get( endpoint(projectId) )
(endpoint is a method that generates the backend api url)
I then have a projectCtrl that is dependent on that factory:
#app.controller 'ProjectCtrl', ['$scope','$routeParams', 'projectFactory', ($scope, $routeParams, projectFactory) ->
$scope.projectId = $routeParams.projectId
$scope.loadProject = (projectId) ->
projectFactory.loadProject(projectId)
.success((data)->$scope.project = data.project)
I then have my project_control_spec test:
'use strict'
describe "ProjectCtrl", ->
beforeEach module 'app'
ProjectCtrl = {}
$scope = {}
projectFactory = {}
beforeEach ->
module($provide) ->
$provide.factory "projectFactory", projectFactory
module inject($controller, $rootScope) ->
$scope = $rootScope.$new()
ProjectCtrl = $controller 'ProjectCtrl', {
$scope : $scope,
$routeParams: {projectId: 1},
}
it "should instantiate a PC", ->
expect(ProjectCtrl).toBeDefined()
it "should have access to the projectId via the routeParams", ->
expect($scope.projectId).toEqual(1)
it "should have access to projectFactory", ->
expect($scope.projectFactory).toBeDefined()
it "should create $scope.project when calling loadProject", ->
expect($scope.project).toBeUndefined();
expect($scope.loadProject(1)).toBe(1)
expect($scope.project).toEqual({//a project object})
I am getting the error ReferenceError: Can't find variable: $provide, when trying to require my projectFactory
You cannot inject $provide on line module inject($controller, $rootScope, $provide) ->. It is also not used or needed in any case.
You should also test this case with $httpBackend. Check the first example.
What if you try this ?
(It is in javascript, sorry I can't write coffee yet)
beforeEach( inject (function( $injector ){
$provide = $injector.get('$provide');
// ...
}));
I'm not familiar with CoffeeScript, but this is how I would do it in plain old JS:
var projectFactory = {};
beforeEach(function () {
module('app', function ($provide) {
$provide.factory('projectFactory', projectFactory);
});
});
When taking some of your code and running it through a Coffee to JS interpreter, I get the following result:
describe("ProjectCtrl", function() {
var $scope, ProjectCtrl, projectFactory;
beforeEach(module('app'));
ProjectCtrl = {};
$scope = {};
projectFactory = {};
beforeEach(function() {
module($provide)(function() {
$provide.factory("projectFactory", projectFactory);
});
});
});
Basically you're trying to load a second module called $provide, when what you actually want to do is open up a config block for the first module (app) and inject $provide into the configuration block.
If you were using angular.module for your actual implementation of app, it'd look something like this:
angular.module('app', []).config(function ($provide) {
$provide.value('..', {});
$provide.constant('...', {});
/** and so on **/
});
Whereas in your specs when using angular-mocks, a config block gets set like this:
module('app', function ($provide) {
$provide.value('..', {});
/** and so on **/
});
I hope that helps. I'm not sure how you would actually write this in CoffeeScript, but I'm sure you can figure that one out. When taking the result of the Coffee to JS interpreter of your code, I received the same result - there is no variable $provide.

Get query string params using Restful / Resourceful / Flatiron

I have the following node app using Restful / Resourceful / Flatiron:
app.js
var flatiron = require('flatiron'),
fixtures = require('./fixtures'),
restful = require('restful'),
resourceful = require('resourceful');
var app = module.exports = flatiron.app;
app.resources = {};
app.resources.Creature = fixtures.Creature;
app.use(flatiron.plugins.http, {
headers: {
'x-powered-by': 'flatiron ' + flatiron.version
}
});
app.use(restful);
app.start(8000, function(){
console.log(app.router.routes);
console.log(' > http server started on port 8000');
console.log(' > visit: http://localhost:8000/ ');
});
Here is the fixtures module:
fixtures.js
var fixtures = exports;
var resourceful = require('resourceful');
// // Create a new Creature resource using the Resourceful library //
fixtures.Creature = resourceful.define('creature', function () {
var self = this;
this.restful = true;
this.all = function (callback) {
console.log(this);
callback(null, "ok"); };
});
How can I access the request/query string parameters? E.g. if the route is /creatures?foo=bar
I came across this issue from the Github repo, but the comments imply there may be a more long winded method of obtaining this data?
I've been looking at the source code for resourceful and I don't see a clear way. Here is the line in question:
https://github.com/flatiron/resourceful/blob/master/lib/resourceful/resource.js#L379
The default versions listed via NPM Package Manager are out of date which caused some confusion.
See the Github issue here:
https://github.com/flatiron/restful/issues/33
Using package.json in combination with NPM install works with the version combinations:
"restful": "0.4.4",
"director": "1.1.x",
"resourceful": "0.3.x",
"director-explorer": "*"
With this newer version the url format now works in the style of:
/create/find?foo=bar
The method in question is can be found here:
https://github.com/flatiron/restful/blob/master/lib/restful.js#L506
At the time of writing the method looks as follows:
router.get('/' + entity + '/find', function () {
var res = this.res,
req = this.req;
preprocessRequest(req, resource, 'find');
resource.find(req.restful.data, function(err, result){
respond(req, res, 200, entity, result);
});
});
The key component being that req.restful.data is the parsed query string data.

Resources