0

I'd like to use the .replace("'", r"\'") method to avoid problems caused by single quotes within JSON strings.. and so that my factory works ...as intended

Simplest solution I could find from This post suggests the replace method, however, beeing new in JS, I have troubles implementing it within my factory.

I am not quite sure where and how to put it, so that any given controller calling different function (especially the getDishes and getDishfunctions at the end of the script below) all get a "formatted string"

Factory :

angular.module('wmapp.factory_dishes', [])

.factory('dishesFactory', function (){
var factory = {
    dishes :[
        {   nameEnglish: 'beef burgungdy', 
            nameLocal: 'boeuf bourgignon',
            description: 'xxxxxx',
            region: 'sicile',
            itemid: 'IT018',
            cuisineTypeIsoCode: 'IT',
            country:'France',
            dishCategory: 'Meat',

        },
        {   nameEnglish: 'duck liver', 
            nameLocal: 'foie gras',
            description: 'xxxxxx',
            region: 'sicile',
            itemid: 'IT021',
            cuisineTypeIsoCode: 'IT',
            country:'France',
            dishCategory: 'fruit',

        },  
        {   nameEnglish: 'veal stew',
            nameLocal: 'blanquette de veau',
            description: 'xxxxxx',  
            region: 'parme',
            itemid: 'IT023',
            cuisineTypeIsoCode: 'IT',
            country:'France',
            dishCategory: 'fruit',

        },
        {   nameEnglish: 'onion soup', 
            nameLocal: 'soxxxxxx',
            region: 'vanitia',
            itemid: 'IT022',
            cuisineTypeIsoCode: 'IT',
            imageSource: '( "img/" + dish.cuisineTypeIsoCode + "/" + dish.itemid + "small.jpg")',
            country:'France',
            dishCategory: 'Soup',
        },

        {  nameEnglish: 'TAPENADE',
           nameLocal: 'Tapenade', 
           description: 'xxxxxx',
           region: 'Provence-Alpes-Côte d'Azur', 
           regioncode: 'FR.B8', 
           itemid: 'FR002', 
           cuisineTypeIsoCode: 'FR', 
           dishCategory: 'Entrée / Appetizers', 
           country: 'France'}




      ], 
      getDishes : function (){
        return factory.dishes;
      },
      getDish :function (itemid){
        var dish = {};
        angular.forEach(factory.dishes, function(value, key) {
            if (value.itemid == itemid){
                dish = value
            }
        });       

        return dish;
      }
}
return factory;

})

Community
  • 1
  • 1
lauWM
  • 659
  • 3
  • 10
  • 18
  • 1
    Have you got those `dishes ` hardcoded inside your factory? If yes the problem is that your js is syntactically invalid. – Yury Tarabanko Mar 24 '15 at 18:56
  • yes for the moment they are hard coded, will set webservices later. I get the json string from an Excel file combining value from a various cell in a single JSON string. I can amend the syntax components , but not the cell content – lauWM Mar 24 '15 at 19:18
  • Well, the question has nothing to do with js or angular. Your code is invalid right now. Escape those quotes manually. – Yury Tarabanko Mar 24 '15 at 19:21
  • there must be a solution to "scan" through an array and replace caracters .. (what is the replace method for then) I can't escape those quotes manually way too many entries – lauWM Mar 24 '15 at 19:26
  • You wont get an array since the syntax is *invalid*. Your js file generate parse error. Means no execution phase. Period. As you can see SO failed to highlight your code sample. – Yury Tarabanko Mar 24 '15 at 21:25
  • And btw why are you calling it JSON? It's not. It is javascript. Invalid javascript. – Yury Tarabanko Mar 24 '15 at 21:27
  • nevermind thanks for your time, i'll search somewhere else – lauWM Mar 24 '15 at 22:22

0 Answers0