Skip to content

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

@thebarty

Description

@thebarty

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions