vincenzomerolla / angular-vimeo-embed

A simple AngularJS directive to embed Vimeo videos easily onto your website.
MIT License
12 stars 9 forks source link

Can't inject Vimeo ID via scope #1

Closed mspriggs closed 9 years ago

mspriggs commented 9 years ago

First of all, thanks for your work on this. I'm wondering if you'd be willing to review this forum post where I'm trying to find an answer to a problem I'm having using your directive. Thanks.

http://forum.ionicframework.com/t/calling-scope-inside-directive/10734

vincenzomerolla commented 9 years ago

It works in this version of my demo

Could you provide a snippet of your javascript?

mspriggs commented 9 years ago

Sure thing. Here is the controller and the two services is calls:

WELSMobile.controller("PlayVideo", ["$scope", "$stateParams", "$cordovaSocialSharing", "$cordovaDevice", "GetJSONData", "BitlyBuilder", PlayVideo]);

function PlayVideo($scope, $stateParams, $cordovaSocialSharing, $cordovaDevice, GetJSONData, BitlyBuilder) {
  var url = "";
  var theID = $stateParams.id;
  var platform = $cordovaDevice.getPlatform();
  var testID = window.location.hash;

  if (platform == 'Android') {
    $scope.showFacebookIcon = false;
  } else {
    $scope.showFacebookIcon = true;
  }

  // determine what feed we are after, then set the URL
  if (testID.search("welstechvideo") > 1) {
    url = "http://vimeo.com/api/v2/album/2039477/videos.json";
  }

    GetJSONData.getJSONFeed(url)
      .then(function(data) {
          $scope.feeddata = data[theID];

            var encodedLink = encodeURIComponent(data[theID].url);
            BitlyBuilder.buildBitlyUrl(encodedLink)
            .then(function(data1) {
            $scope.shortURL = data1.data.url;
            });

      }, function(error) {
          console.error('Error fetching feed:', error);
        });  
}

WELSMobile.factory('GetJSONData', function ($http, $q) {
    return {
        getJSONFeed: function(link1) {
            var deferred = $q.defer();

            $http.get(link1)
                .success(function(data) {
                    if (typeof data === 'object') {
                        deferred.resolve(data);
                    } else {
                        deferred.reject(data);
                    }
                }).error(function(data, status, headers, config) {
                    deferred.reject(data);
                });
            return deferred.promise;
        }
    };
});

WELSMobile.factory('BitlyBuilder', function ($http, $q) {
    return {
        buildBitlyUrl: function(link1) {
            var deferred = $q.defer();
            $http({ method: 'GET', url: 'https://api-ssl.bitly.com/v3/shorten?access_token=e4a8fcbe25ca4dc7bb59d00797ee9cd11792cda6&longUrl='+link1})
                .success(function(data) {
                    if (typeof data === 'object') {
                        deferred.resolve(data);
                    } else {
                        deferred.reject(data);
                    }
                }).error(function(data, status, headers, config) {
                    deferred.reject(data);
                });
            return deferred.promise;
        }
    };
});
vincenzomerolla commented 9 years ago

Updated your comment with Markdown for better legibility

vincenzomerolla commented 9 years ago

Are you sure you're getting the correct data in feeddata? I think your problem may lie in the line below

$scope.feeddata = data[theID];

It looks like your trying to do a lookup in the array by id, but this is not possible without the use of a library like Underscore or Lodash which provide various array utility functions. Lodash's find function returns the object that you may be looking for.

Here's how you would go about doing that:

$scope.feeddata = _.find(data, {'id': theID});
mspriggs commented 9 years ago

I don't think that is the case. When I put the scope variable in a separate div it shows me the correct id. It's just when its in the directive that it fails. On Oct 8, 2014 12:52 AM, "Vincenzo Merolla" notifications@github.com wrote:

Are you sure you're getting the correct data in feeddata? I think your problem may lie in the line below

$scope.feeddata = data[theID];

It looks like your trying to do a lookup in the array by id, but this is not possible without the use of a library like Underscore http://underscorejs.org/ or Lodash http://lodash.com which provide functions that return the object you are looking for.

Here's how you would go about doing that:

$scope.feeddata = _.find(data, {'id': theID});

— Reply to this email directly or view it on GitHub https://github.com/vincenzomerolla/angular-vimeo-embed/issues/1#issuecomment-58313690 .

vincenzomerolla commented 9 years ago

Can you provide a demo on JSFiddle, JSBin or Plunker? Maybe I can figure it out.

vincenzomerolla commented 9 years ago

@mspriggs Did you get it working? If yes, I will close the issue.

vincenzomerolla commented 9 years ago

Closing this issue.