2014-02-06 50 views
0

我正在嘗試構建一個具有以下功能的Web應用程序:在某個時間點,用戶將能夠單擊一個按鈕來爲他的頁面添加一個「框」(帶有2個輸入字段)。每個用戶可以添加多個「盒子」,使1個用戶可能有2個他的網頁上,我的另一個具有5.我使用jQuery來做到這一點,我的邏輯是這樣的:「增加」如何在Python金字塔框架中獲取JavaScript數組?

  • 用戶點擊按鈕
  • 一個html元素被添加到一個空數組作爲字符串
  • 我這個數組發送到數據庫
  • 當用戶回來,我檢索數據庫的陣列,然後重新添加jQuery中

現在我設法做了第一個2 po整數(最容易= D),但我堅持在未來2.

我正在使用金字塔框架爲這個應用程序,我認爲應該有一種方法來獲取金字塔中的數組,所以我可以發送它到數據庫,但我無法弄清楚。

請你指點一些文檔,或者我可以從中學到如何操作的其他東西?

HTML代碼:

<!DOCTYPE html> 
<html> 

<head> 
    <link type= "text/css" rel="stylesheet" href="style.css"/> 
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script> 
    <script type="text/javascript" src='script.js'></script> 
</head> 

<body> 
    <div class='add'></div> 
    </div> 
</body> 

</html> 

的CSS是這樣的:

.add { 
    border: 1px solid black; 
    width: 30%; 
    height: 40px; 
    margin: auto; 
    margin-top: 20%; 
    position: relative; 
} 

.room { 
    height: 150px; 
    width: 40%; 
    border: 1px solid black; 
    margin: auto; 
    margin-bottom: 1%; 
    position:relative; 
} 

body{ 
    height: 100%; 
    width: 100%; 
} 

和jQuery代碼:

var roomlist = new Array() 

$(document).ready(function(){ 
    $('.add').click(function(){ 
     roomlist.push('<div class="room"></div>') 
     $('.add').before(roomlist[roomlist.length -1]); 
     $('.add').css("margin-top","1%"); 
    }); 
}); 
+0

波格丹,你絕對可以做到這一點。但是,請顯示您當前的jQuery,html和python代碼。 – Raj

+0

@Raj不幸的是,目前,我還沒有任何Python代碼,因爲我還在學金字塔。 我的問題是,我無法告訴(從我讀到目前爲止)我可以使用什麼方法/技術從靜態js文件中獲取「roomlist」並將其作爲變量添加到金字塔中。 我的方法是,我考慮設計,找出可能遇到的問題,學習修復它們的工具...編寫實際代碼 –

+0

這是您希望在*客戶端*上發生的事情(意思是在用戶的瀏覽器中)? http://jsfiddle.net/K2EzP/ – Raj

回答

0

在Python中的金字塔,你會得到JSON渲染,你可以做到這一點非常簡單地。在金字塔中,您可以使用GET,PUT,POST和DELETE方法來實現寧靜的Web服務。 通過使用GET方法,您可以在jquery中獲得您的列表,並使用POST方法將您的列表保存在數據庫中。

Ajax Development With JSON Renderers

示例GET,PUT和POST方法

@view_defaults(route_name='myapi', renderer='json') 
class GroupView(object): 
    def __init__(self, request): 
     self.request = request 

    @view_config(request_method='GET') 
    def get(self): 
    # return information about the list or objest 

    @view_config(request_method='PUT') 
    def put(self): 
     # update list or object 

    @view_config(request_method='POST') 
    def post(self): 
     # Create new list or object