将变量传递给函数以使服务器识别类别和ID

时间:2014-03-04 11:04:53

标签: jquery grails

HTML

<a class="bookmarkit" herf="#modalBookmark" role="button" data-id="${image.id}" data-type="image">Bookmark</a>
<div id="modalBookmark" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header pure-u-1">
    <a href="javascript:void(0);" class="close" data-dismiss="modal" aria-hidden="true"><i class="icn-close-modal"></i></a>
    <h3 class="t-center">Add to Bookmark List</h3>
  </div>
  <div class="modal-body pure-u-1 log-body">

    <div class="bookmark-new">
      <form class="pure-form pure-g frm-add-list">
        <div class="pure-u-5-6"><input name="list" type="text" /></div>
        <div class="pure-u-1-6"><button class="pure-button btn-cromly btn-add-list">Add</button></div>
      </form>
    </div>
    <div class="bookmark-list-wrapper">

    </div>
    <div class="bookmark-actions">
      <a href="javascript:void(0);" class="pure-button btn-cromly btn-add" data-dismiss="modal" aria-hidden="true">Save</a>
    </div>
  </div>
</div>

jquery的

 var entryid = $('.bookmarkit').data('id');
    var category = $('.bookmarkit').data('type');
    $('#modalBookmark')
        .appendTo(document.body)
        .on('shown', function() {

            getBookmarks(userId, entryid);


        });

    $('.btn-add-list').click(function(e) {
        e.preventDefault();
        var bookmarkName = $('.bookmark-new input[name="list"]').val();
        console.log(bookmarkName);
        var bookmark = {
            ownerId: userId,
            name: bookmarkName
        };
        addBookmark(bookmark);

    });

    $('.bookmark-list-wrapper').on('click', '.bookmark-link', function(e) {
        e.preventDefault();
        addBookmarkItem($(this), category, entryid, $(this).parent().attr('data-bookmark-id'));
    });



    function getBookmarks(userID, itemID) {
        $.ajax({
            url: '/api/bookmarks',
            type: 'GET',
            data: {
                ownerId: userID,
                itemId: itemID,

            },
            success: function(response) {
                console.log(response);

                $('.bookmark-list-wrapper').html(can.view('/views/bookmarks/bookmarks.ejs', {
                    bookmarks: response
                }));

            }
        });
    }

    function addBookmark(bookmarkObj) {

        $.ajax({
            url: '/api/bookmarks',
            type: 'POST',
            data: bookmarkObj,
            success: function(response) {
                $('.bookmark-list').prepend(can.view('/views/bookmarks/bookmark.ejs', {
                    bookmark: response
                }));
            }
        });
    }



    function addBookmarkItem(btn, category, itemID, bookmarkID) {
        var sendData = {
            ownerId: userId,
        };

        console.log('?: ' + $.param(sendData));

        $.ajax({
            url: '/api/bookmarks/' + bookmarkID + '?' + category +"="+ itemID + $.param(sendData),
            type: 'PUT',
            contentType: 'application/json',
            success: function(response) {
                console.log(response);
                btn.addClass('active');
                btn.find('.count').text(response.items.length);
            }
        });
    }

url: '/api/bookmarks/' + bookmarkID + '?' + category +"="+ itemID + $.param(sendData),似乎不对..

动态获取catagory的数据类型,即投资组合,图像等,并获取条目ID的数据ID。然后grails(控制器)将检查是否指定了类别,然后他们将它传递给书签功能: 1)获取类别,条目ID,用户ID。 2)将它们传递给显示的功能,显示列表等。

现在我希望书签列表中的书签更新为具有entry-id和user-id的特定类别。 例如:http:/ bookmarks?portfolio = 123&amp; userId = 7 我试图找到方法,但我得到错误所以我粘贴没有更改。

更新

我在console.log上看到错误 - (不可处理的实体)。不确定它是什么。 单击addBookmarkItem按钮时,书签将标记为条目ID和类别。

有没有办法获得/书签?portfolio = 123&amp; userId = 7以正确的方式工作?

如果您需要查看书签api控制器:

def update(Bookmark bookmark) {
    log.debug "Updating bookmark - bookmark=${bookmark}, params=${params}"

    if (params.image) {
        bookmark.addToImages(params.image)
    } else if (params.portfolio) {
        bookmark.addToPortfolios(params.portfolio)
    } else if (params.catalogue) {
        bookmark.addToCatalogues(params.catalogue)
    }

    try {
        println " Validate? " + bookmark.validate()
        bookmark.save(failOnError:true, flush:true)
    } catch (e) {
        e.printStackTrace()
        log.error e
        respond e
        return
    }

    def item = [:]
    item.name = bookmark.name
    item.id = bookmark.id
    item.items = bookmark.items ?: []
    item.portfolios = bookmark.portfolios ?: []
    item.added = true
    portfolios=${bookmark.portfolios}" 

    respond item 
}

1 个答案:

答案 0 :(得分:0)

当您通过模型将变量传递到视图e.G

def bookmarkId = "fooBar"
render(view: 'myView', model:[bookmarkId: bookmarkId])

然后你可以将它传递到你的javascript中:

    $.ajax({
        <g:if test="${bookmarkId}">
        url: '/api/bookmarks/${bookmarkId}?' + category +"="+ itemID + $.param(sendData),
        </g:if>
        <g:else>
        url: '/api/bookmarks/?' + category +"="+ itemID + $.param(sendData),
        </g:else>
        type: 'PUT',
        contentType: 'application/json',
        success: function(response) {
            console.log(response);
            btn.addClass('active');
            btn.find('.count').text(response.items.length);
        }
    });

您可以使用“普通”gsp脚本来检查条件是否匹配等。

也许你有个主意

欢呼声