码迷,mamicode.com
首页 > 其他好文 > 详细

-_-#【Backbone】Collection

时间:2014-12-18 16:27:38      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   io   color   sp   on   div   art   

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="../../app-min.css">
</head>
<body>
    <script src="../../jquery/jquery-1.10.2.js"></script>
    <script src="../underscore.js"></script>
    <script src="../backbone.js"></script>
    <script>
        var Book = Backbone.Model.extend({
            defaults: {
                title: default
            },
            initialize: function() {
            }
        })
        var BookShelf = Backbone.Collection.extend({
            model: Book
        })
        var book1 = new Book({title: book1})
        var book2 = new Book({title: book2})
        var book3 = new Book({title: book3})
        var bookShelf = new BookShelf
        bookShelf.add(book1)
        bookShelf.add(book2)
        bookShelf.add(book3)
        bookShelf.remove(book3)
        bookShelf.each(function(book) {
            console.log(book.get(title))
        })

        var showAllBooks = function() {
            bookShelf.each(function(book) {
                console.log(book.get(title))
            })
        }

        bookShelf.bind(reset, showAllBooks)
        bookShelf.url = /books/
        bookShelf.fetch({
            reset: true,
            success: function(collection, response, options) {
                collection.each(function(book) {
                    console.log(book.get(title))
                })
            },
            error: function(collection, response, options) {
                console.log(error)
            }
        })

        var NewBooks = Backbone.Collection.extend({
            model: Book,
            url: /books/
        })
        var books = new NewBooks
        var onebook = books.create({
            title: "I‘m coming"
        })
    </script>
    <article class="content">
        <pre>var Book = Backbone.Model.extend({
    defaults: {
        title: ‘default‘
    },
    initialize: function() {
    }
})
var BookShelf = Backbone.Collection.extend({
    model: Book
})
var book1 = new Book({title: ‘book1‘})
var book2 = new Book({title: ‘book2‘})
var book3 = new Book({title: ‘book3‘})
var bookShelf = new BookShelf
bookShelf.add(book1)
bookShelf.add(book2)
bookShelf.add(book3)
bookShelf.remove(book3)
bookShelf.each(function(book) {
    console.log(book.get(‘title‘))
})

var showAllBooks = function() {
    bookShelf.each(function(book) {
        console.log(book.get(‘title‘))
    })
}

bookShelf.bind(‘reset‘, showAllBooks)
bookShelf.url = ‘/books/‘
bookShelf.fetch({
    reset: true,
    success: function(collection, response, options) {
        collection.each(function(book) {
            console.log(book.get(‘title‘))
        })
    },
    error: function(collection, response, options) {
        console.log(‘error‘)
    }
})

var NewBooks = Backbone.Collection.extend({
    model: Book,
    url: ‘/books/‘
})
var books = new NewBooks
var onebook = books.create({
    title: "I‘m coming"
})</pre>
    </article>
</body>
</html>

 

-_-#【Backbone】Collection

标签:style   blog   ar   io   color   sp   on   div   art   

原文地址:http://www.cnblogs.com/jzm17173/p/4171835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!