Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please post some better examples on how to use this #23

Open
thebarty opened this issue Sep 15, 2015 · 0 comments
Open

Please post some better examples on how to use this #23

thebarty opened this issue Sep 15, 2015 · 0 comments

Comments

@thebarty
Copy link

Hi guys,

first of all: thanks a lot for this cool package.

I am just starting out with angular and I could really use some good examples on how to use this resource.

  1. How do I register it as a factory?
  2. How do I do a simple INSERT when listening to a form (for example add a new todo item to a todo-list).
  3. How do I do a eimple UPDATE (for example when editing an existing todo item to a todo-list).

Could somebody help me out?

For question 1) I think I have found the way to do it::

// services.js
var todoServices = angular.module('todoServices', ['djangoRESTResources'])
    .factory('Todo', function (djResource) { 
            return djResource('/api/todos/:todoId/', {todoId:'@id'});
        }
    );

Right now I am stuck with question 2). How do I insert a new item? This is my current code (NOT working):

<form ng-submit="TodoListCtrl.addTodo(todo)" novalidate>
              <div class="form-group">
                <label for="exampleInputEmail1">Description</label>
                <input ng-model="todo.description" id="todoDescription" type="text" class="form-control" placeholder="Todo">
              </div>
             <div class="checkbox">
                <label>
                  <input ng-model="todo.is_done" id="todoIsDone" type="checkbox"> ist done?
                </label>
              </div>
              <button ng-click="add(todo)" type="submit" class="btn btn-default">submit</button>
            </form>
// controllers.js
todoControllers.controller('TodoListCtrl', ['$scope', 'Todo',
    function ($scope, Todo) {
        $scope.todos = Todo.query();

        $scope.add = function(todo) {
            var newTodo = new Todo({description:todo.description, is_done:todo.is_done});
            newTodo.$save();
        }
    }
]);

Any ideas how to do this?

@thebarty thebarty changed the title Please post some better Examples on how to use this Please post some better examples on how to use this Sep 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant