Skip to content

Instantly share code, notes, and snippets.

@OdeToCode
Last active December 14, 2015 03:59

Revisions

  1. OdeToCode revised this gist Feb 28, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -54,7 +54,7 @@
    );
    });

    var VideoController = function ($scope, $http, Video) {
    var VideoController = function ($scope Video) {


    var createVideo = function (newVideo) {
  2. OdeToCode revised this gist Feb 28, 2013. 1 changed file with 31 additions and 41 deletions.
    72 changes: 31 additions & 41 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    <div ng-app ng-controller="VideoController">
    <div ng-app="videoApp" ng-controller="VideoController">

    <table>
    <thead>
    @@ -7,12 +7,12 @@
    <th></th>
    </thead>
    <tbody>
    <tr ng-repeat="video in videos">
    <tr data-id="{{video.Id}}" ng-repeat="video in videos">
    <td>{{video.Title}}</td>
    <td>{{video.Length}}</td>
    <td>
    <button ng-click="editVideo(video.Id)">Edit</button>
    <button ng-click="deleteVideo(video.Id)">Delete</button>
    <button ng-click="editVideo(video)">Edit</button>
    <button ng-click="deleteVideo(video)">Delete</button>
    </td>
    </tr>
    </tbody>
    @@ -42,38 +42,33 @@
    <script src="~/Scripts/underscore.js"></script>

    <script>

    var VideoController = function ($scope, $http) {

    var serviceUrl = "/api/videos";

    var blankVideo = {
    Id: 0,
    Title: "",
    Length: 75
    };

    var videoFinder = function(id) {
    return function(video) {
    return video.Id == id;
    };
    };

    angular.module("videoApp", ["videoService"]);

    angular.module("videoService", ["ngResource"]).
    factory("Video", function ($resource) {
    return $resource(
    "/api/videos/:Id",
    {Id: "@@Id" },
    { "update": {method:"PUT"} }
    );
    });

    var VideoController = function ($scope, $http, Video) {


    var createVideo = function (newVideo) {
    $http.post(serviceUrl, newVideo)
    .success(function (video) {
    $scope.videos.push(video);
    });
    newVideo.$save();
    $scope.videos.push(newVideo);
    };

    var updateVideo = function(video) {
    $http.put(serviceUrl + "/" + video.Id, video);
    };
    var updateVideo = function(video) {
    video.$update();
    };


    $scope.showEdit = function () {
    $scope.isEditVisible = true;
    $scope.editableVideo = angular.copy(blankVideo);
    $scope.editableVideo = new Video();
    };

    $scope.saveVideo = function (video) {
    @@ -86,23 +81,18 @@
    }
    };

    $scope.editVideo = function (id) {
    $scope.editVideo = function (video) {
    $scope.isEditVisible = true;
    $scope.editableVideo = _.find($scope.videos, videoFinder(id));
    $scope.editableVideo = video;
    };

    $scope.deleteVideo = function (id) {
    $http.delete(serviceUrl + "/" + id)
    .success(function () {
    $scope.videos = _.reject($scope.videos, videoFinder(id));
    });
    $scope.deleteVideo = function (video) {
    video.$delete();
    $scope.videos = _.without($scope.videos, video);
    };

    $scope.isEditVisible = false;
    $http.get(serviceUrl).success(function (data) {
    $scope.videos = data;
    });
    $scope.videos = Video.query();
    };

    </script>
    }
    </script>
  3. OdeToCode revised this gist Feb 25, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    <th></th>
    </thead>
    <tbody>
    <tr data-id="{{video.Id}}" ng-repeat="video in videos">
    <tr ng-repeat="video in videos">
    <td>{{video.Title}}</td>
    <td>{{video.Length}}</td>
    <td>
  4. OdeToCode created this gist Feb 24, 2013.
    108 changes: 108 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,108 @@
    <div ng-app ng-controller="VideoController">

    <table>
    <thead>
    <th>Title</th>
    <th>Length</th>
    <th></th>
    </thead>
    <tbody>
    <tr data-id="{{video.Id}}" ng-repeat="video in videos">
    <td>{{video.Title}}</td>
    <td>{{video.Length}}</td>
    <td>
    <button ng-click="editVideo(video.Id)">Edit</button>
    <button ng-click="deleteVideo(video.Id)">Delete</button>
    </td>
    </tr>
    </tbody>
    </table>

    <button ng-click="showEdit()">Create Video</button>

    <div ng-show="isEditVisible">
    <hr />
    <form>
    <input type="hidden" ng-model="editableVideo.Id" />
    <label>Title:</label>
    <input type="text" ng-model="editableVideo.Title" required />
    <label>Length</label>
    <input type="number" ng-model="editableVideo.Length" min="1" max="360" />
    <input type="submit" value="Save" ng-click="saveVideo(editableVideo)" />
    </form>
    </div>

    </div>


    @section scripts{

    <script src="~/Scripts/angular.js"></script>
    <script src="~/Scripts/angular-resource.js"></script>
    <script src="~/Scripts/underscore.js"></script>

    <script>

    var VideoController = function ($scope, $http) {

    var serviceUrl = "/api/videos";

    var blankVideo = {
    Id: 0,
    Title: "",
    Length: 75
    };

    var videoFinder = function(id) {
    return function(video) {
    return video.Id == id;
    };
    };

    var createVideo = function (newVideo) {
    $http.post(serviceUrl, newVideo)
    .success(function (video) {
    $scope.videos.push(video);
    });
    };

    var updateVideo = function(video) {
    $http.put(serviceUrl + "/" + video.Id, video);
    };


    $scope.showEdit = function () {
    $scope.isEditVisible = true;
    $scope.editableVideo = angular.copy(blankVideo);
    };

    $scope.saveVideo = function (video) {
    $scope.isEditVisible = false;
    if (video.Id) {
    updateVideo(video);
    }
    else {
    createVideo(video);
    }
    };

    $scope.editVideo = function (id) {
    $scope.isEditVisible = true;
    $scope.editableVideo = _.find($scope.videos, videoFinder(id));
    };

    $scope.deleteVideo = function (id) {
    $http.delete(serviceUrl + "/" + id)
    .success(function () {
    $scope.videos = _.reject($scope.videos, videoFinder(id));
    });
    };

    $scope.isEditVisible = false;
    $http.get(serviceUrl).success(function (data) {
    $scope.videos = data;
    });
    };

    </script>
    }